blob: 6017e8be181f8d5e95a1a62986ee838806a7cf08 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 Das4c35fc42015-11-20 15:27:53 -080018import org.onlab.packet.EthType;
sangho80f11cb2015-04-01 13:05:26 -070019import org.onlab.packet.Ethernet;
20import org.onlab.packet.Ip4Address;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070021import org.onlab.packet.Ip4Prefix;
sangho80f11cb2015-04-01 13:05:26 -070022import org.onlab.packet.IpPrefix;
23import org.onlab.packet.MacAddress;
sangho80f11cb2015-04-01 13:05:26 -070024import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070025import org.onlab.packet.VlanId;
Charles Chanb7f75ac2016-01-11 18:28:54 -080026import org.onosproject.net.ConnectPoint;
Charles Chan319d1a22015-11-03 10:42:14 -080027import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
28import org.onosproject.segmentrouting.config.DeviceConfiguration;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070029import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sangho80f11cb2015-04-01 13:05:26 -070030import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070031import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070032import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070033import org.onosproject.net.flow.DefaultTrafficSelector;
34import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070035import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070037import org.onosproject.net.flow.criteria.Criteria;
38import org.onosproject.net.flowobjective.DefaultFilteringObjective;
39import org.onosproject.net.flowobjective.DefaultForwardingObjective;
40import org.onosproject.net.flowobjective.FilteringObjective;
41import org.onosproject.net.flowobjective.ForwardingObjective;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -070042import org.onosproject.net.flowobjective.Objective;
43import org.onosproject.net.flowobjective.ObjectiveError;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070044import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das9f1c42e2015-10-23 10:51:11 -070045import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -070046import org.onosproject.net.flowobjective.ObjectiveContext;
sangho80f11cb2015-04-01 13:05:26 -070047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
50import java.util.ArrayList;
Saurav Dasc28b3432015-10-30 17:45:38 -070051import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070052import java.util.List;
Charles Chanb7f75ac2016-01-11 18:28:54 -080053import java.util.Map;
sangho80f11cb2015-04-01 13:05:26 -070054import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070055import java.util.concurrent.atomic.AtomicLong;
sangho80f11cb2015-04-01 13:05:26 -070056
57import static com.google.common.base.Preconditions.checkNotNull;
58
Charles Chanb7f75ac2016-01-11 18:28:54 -080059/**
60 * Populator of segment routing flow rules.
61 */
sangho80f11cb2015-04-01 13:05:26 -070062public class RoutingRulePopulator {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070063 private static final Logger log = LoggerFactory
64 .getLogger(RoutingRulePopulator.class);
sangho80f11cb2015-04-01 13:05:26 -070065
sanghofb7c7292015-04-13 15:15:58 -070066 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070067 private SegmentRoutingManager srManager;
68 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070069
70 private static final int HIGHEST_PRIORITY = 0xffff;
Charles Chanb7f75ac2016-01-11 18:28:54 -080071 //
72 private static final int XCONNECT_PRIORITY = 1000;
73 private static final int DEFAULT_PRIORITY = 100;
74 private static final int FLOOD_PRIORITY = 5;
Saurav Das7c305372015-10-28 12:39:42 -070075 private static final long OFPP_MAX = 0xffffff00L;
76
Saurav Das9f1c42e2015-10-23 10:51:11 -070077
sangho80f11cb2015-04-01 13:05:26 -070078 /**
79 * Creates a RoutingRulePopulator object.
80 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070081 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070082 */
83 public RoutingRulePopulator(SegmentRoutingManager srManager) {
84 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070085 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-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 Vachuska7cfc6202015-04-30 18:13:25 -070098 *
99 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -0700100 */
101 public long getCounter() {
102 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -0700103 }
104
105 /**
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700106 * Populates IP flow rules for specific hosts directly connected to the
107 * switch.
sangho80f11cb2015-04-01 13:05:26 -0700108 *
109 * @param deviceId switch ID to set the rules
110 * @param hostIp host IP address
111 * @param hostMac host MAC address
112 * @param outPort port where the host is connected
113 */
114 public void populateIpRuleForHost(DeviceId deviceId, Ip4Address hostIp,
115 MacAddress hostMac, PortNumber outPort) {
Charles Chanf4586112015-11-09 16:37:23 -0800116 log.debug("Populate IP table entry for host {} at {}:{}",
117 hostIp, deviceId, outPort);
118 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800119 try {
Charles Chanf4586112015-11-09 16:37:23 -0800120 fwdBuilder = getForwardingObjectiveBuilder(
121 deviceId, hostIp, hostMac, outPort);
Charles Chan319d1a22015-11-03 10:42:14 -0800122 } catch (DeviceConfigNotFoundException e) {
123 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
124 return;
125 }
Charles Chanf4586112015-11-09 16:37:23 -0800126 srManager.flowObjectiveService.
127 forward(deviceId, fwdBuilder.add(new SRObjectiveContext(deviceId,
128 SRObjectiveContext.ObjectiveType.FORWARDING)));
129 rulePopulationCounter.incrementAndGet();
130 }
131
Charles Chanb7f75ac2016-01-11 18:28:54 -0800132 /**
133 * Removes IP rules for host when the host is gone.
134 *
135 * @param deviceId device ID of the device that host attaches to
136 * @param hostIp IP address of the host
137 * @param hostMac MAC address of the host
138 * @param outPort port that host attaches to
139 */
Charles Chanf4586112015-11-09 16:37:23 -0800140 public void revokeIpRuleForHost(DeviceId deviceId, Ip4Address hostIp,
141 MacAddress hostMac, PortNumber outPort) {
142 log.debug("Revoke IP table entry for host {} at {}:{}",
143 hostIp, deviceId, outPort);
144 ForwardingObjective.Builder fwdBuilder;
145 try {
146 fwdBuilder = getForwardingObjectiveBuilder(
147 deviceId, hostIp, hostMac, outPort);
148 } catch (DeviceConfigNotFoundException e) {
149 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
150 return;
151 }
152 srManager.flowObjectiveService.
153 forward(deviceId, fwdBuilder.remove(new SRObjectiveContext(deviceId,
154 SRObjectiveContext.ObjectiveType.FORWARDING)));
155 }
156
157 private ForwardingObjective.Builder getForwardingObjectiveBuilder(
158 DeviceId deviceId, Ip4Address hostIp,
159 MacAddress hostMac, PortNumber outPort)
160 throws DeviceConfigNotFoundException {
161 MacAddress deviceMac;
162 deviceMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800163
sangho80f11cb2015-04-01 13:05:26 -0700164 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
165 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
166
sangho80f11cb2015-04-01 13:05:26 -0700167 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Saurav Das2d94d312015-11-24 23:21:05 -0800168 sbuilder.matchIPDst(IpPrefix.valueOf(hostIp, IpPrefix.MAX_INET_MASK_LENGTH));
169 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700170
sangho27462c62015-05-14 00:39:53 -0700171 tbuilder.deferred()
172 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800173 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700174 .setOutput(outPort);
sangho80f11cb2015-04-01 13:05:26 -0700175 TrafficTreatment treatment = tbuilder.build();
Saurav Das2d94d312015-11-24 23:21:05 -0800176
177 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
178 // for switch pipelines that need it, provide outgoing vlan as metadata
179 VlanId outvlan = null;
180 Ip4Prefix subnet = srManager.deviceConfiguration.getPortSubnet(deviceId, outPort);
181 if (subnet == null) {
182 outvlan = VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET);
183 } else {
184 outvlan = srManager.getSubnetAssignedVlanId(deviceId, subnet);
185 }
186 TrafficSelector meta = DefaultTrafficSelector.builder()
187 .matchVlanId(outvlan).build();
188 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
189 treatment, meta);
sangho80f11cb2015-04-01 13:05:26 -0700190
Charles Chanf4586112015-11-09 16:37:23 -0800191 return DefaultForwardingObjective.builder()
Saurav Das2d94d312015-11-24 23:21:05 -0800192 .withSelector(selector)
193 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800194 .fromApp(srManager.appId).makePermanent()
Charles Chanb7f75ac2016-01-11 18:28:54 -0800195 .withPriority(DEFAULT_PRIORITY).withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700196 }
197
198 /**
199 * Populates IP flow rules for the subnets of the destination router.
200 *
201 * @param deviceId switch ID to set the rules
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700202 * @param subnets subnet information
sangho80f11cb2015-04-01 13:05:26 -0700203 * @param destSw destination switch ID
204 * @param nextHops next hop switch ID list
205 * @return true if all rules are set successfully, false otherwise
206 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700207 public boolean populateIpRuleForSubnet(DeviceId deviceId,
Charles Chanc6ad7752015-10-29 14:58:10 -0700208 Set<Ip4Prefix> subnets,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700209 DeviceId destSw,
210 Set<DeviceId> nextHops) {
sangho80f11cb2015-04-01 13:05:26 -0700211
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700212 for (IpPrefix subnet : subnets) {
sangho80f11cb2015-04-01 13:05:26 -0700213 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
214 return false;
215 }
216 }
217
218 return true;
219 }
220
221 /**
222 * Populates IP flow rules for the router IP address.
223 *
Saurav Das88979182015-10-19 14:37:36 -0700224 * @param deviceId target device ID to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700225 * @param ipPrefix the IP address of the destination router
226 * @param destSw device ID of the destination router
227 * @param nextHops next hop switch ID list
228 * @return true if all rules are set successfully, false otherwise
229 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700230 public boolean populateIpRuleForRouter(DeviceId deviceId,
231 IpPrefix ipPrefix, DeviceId destSw,
232 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-11-03 10:42:14 -0800233 int segmentId;
234 try {
235 segmentId = config.getSegmentId(destSw);
236 } catch (DeviceConfigNotFoundException e) {
237 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
238 return false;
239 }
sangho80f11cb2015-04-01 13:05:26 -0700240
241 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
sangho80f11cb2015-04-01 13:05:26 -0700242 sbuilder.matchIPDst(ipPrefix);
243 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chanf4586112015-11-09 16:37:23 -0800244 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700245
Charles Chanf4586112015-11-09 16:37:23 -0800246 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
247 NeighborSet ns;
248 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700249
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700250 // If the next hop is the same as the final destination, then MPLS label
251 // is not set.
sangho80f11cb2015-04-01 13:05:26 -0700252 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chanf4586112015-11-09 16:37:23 -0800253 tbuilder.immediate().decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700254 ns = new NeighborSet(nextHops);
Charles Chanf4586112015-11-09 16:37:23 -0800255 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700256 } else {
Charles Chan319d1a22015-11-03 10:42:14 -0800257 ns = new NeighborSet(nextHops, segmentId);
Charles Chanf4586112015-11-09 16:37:23 -0800258 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700259 }
260
Saurav Das4c35fc42015-11-20 15:27:53 -0800261 // setup metadata to pass to nextObjective - indicate the vlan on egress
262 // if needed by the switch pipeline. Since neighbor sets are always to
263 // other neighboring routers, there is no subnet assigned on those ports.
264 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
265 metabuilder.matchVlanId(
266 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
267
268 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
269 if (nextId <= 0) {
sangho2165d222015-05-01 09:38:25 -0700270 log.warn("No next objective in {} for ns: {}", deviceId, ns);
271 return false;
272 }
273
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700274 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
275 .builder()
276 .fromApp(srManager.appId)
277 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800278 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700279 .withSelector(selector)
Flavio Castro99c8dfb2016-01-14 14:53:33 -0800280 .withPriority(2000 * ipPrefix.prefixLength())
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700281 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800282 if (treatment != null) {
283 fwdBuilder.withTreatment(treatment);
284 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700285 log.debug("Installing IPv4 forwarding objective "
sangho2165d222015-05-01 09:38:25 -0700286 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700287 ipPrefix,
288 deviceId);
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700289 srManager.flowObjectiveService.
290 forward(deviceId,
291 fwdBuilder.
292 add(new SRObjectiveContext(deviceId,
293 SRObjectiveContext.ObjectiveType.FORWARDING)));
sanghofb7c7292015-04-13 15:15:58 -0700294 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700295
296 return true;
297 }
298
sangho80f11cb2015-04-01 13:05:26 -0700299 /**
Saurav Das88979182015-10-19 14:37:36 -0700300 * Populates MPLS flow rules to all routers.
sangho80f11cb2015-04-01 13:05:26 -0700301 *
Saurav Das88979182015-10-19 14:37:36 -0700302 * @param deviceId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700303 * @param destSwId destination switch device ID
304 * @param nextHops next hops switch ID list
305 * @return true if all rules are set successfully, false otherwise
306 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700307 public boolean populateMplsRule(DeviceId deviceId, DeviceId destSwId,
308 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-11-03 10:42:14 -0800309 int segmentId;
310 try {
311 segmentId = config.getSegmentId(destSwId);
312 } catch (DeviceConfigNotFoundException e) {
313 log.warn(e.getMessage() + " Aborting populateMplsRule.");
314 return false;
315 }
sangho80f11cb2015-04-01 13:05:26 -0700316
317 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU47b8aa22015-09-11 11:19:11 -0700318 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700319
320 // TODO Handle the case of Bos == false
sangho80f11cb2015-04-01 13:05:26 -0700321 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
Saurav Das4c35fc42015-11-20 15:27:53 -0800322 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
Charles Chande6655c2015-12-23 00:15:11 -0800323 sbuilder.matchMplsBos(true);
Saurav Das4c35fc42015-11-20 15:27:53 -0800324 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700325
Saurav Das4c35fc42015-11-20 15:27:53 -0800326 // setup metadata to pass to nextObjective - indicate the vlan on egress
327 // if needed by the switch pipeline. Since mpls next-hops are always to
328 // other neighboring routers, there is no subnet assigned on those ports.
329 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
330 metabuilder.matchVlanId(
331 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
332
333 // If the next hop is the destination router for the segment, do pop
sangho80f11cb2015-04-01 13:05:26 -0700334 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700335 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das4c35fc42015-11-20 15:27:53 -0800336 + "label {} in switch {} with pop", segmentId, deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700337
Saurav Das4c35fc42015-11-20 15:27:53 -0800338 // bos pop case (php)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700339 ForwardingObjective.Builder fwdObjBosBuilder =
340 getMplsForwardingObjective(deviceId,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700341 nextHops,
342 true,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700343 true,
Saurav Das4c35fc42015-11-20 15:27:53 -0800344 metabuilder.build());
345 if (fwdObjBosBuilder == null) {
sangho80f11cb2015-04-01 13:05:26 -0700346 return false;
347 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800348 fwdObjBuilders.add(fwdObjBosBuilder);
349
350 // XXX not-bos pop case, SR app multi-label not implemented yet
351 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
352 getMplsForwardingObjective(deviceId,
353 nextHops,
354 true,
355 false);*/
356
sangho80f11cb2015-04-01 13:05:26 -0700357 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800358 // next hop is not destination, SR CONTINUE case (swap with self)
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700359 log.debug("Installing MPLS forwarding objective for "
Saurav Das4c35fc42015-11-20 15:27:53 -0800360 + "label {} in switch {} without pop", segmentId, deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700361
Saurav Das4c35fc42015-11-20 15:27:53 -0800362 // continue case with bos - this does get triggered in edge routers
363 // and in core routers - driver can handle depending on availability
364 // of MPLS ECMP or not
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700365 ForwardingObjective.Builder fwdObjBosBuilder =
366 getMplsForwardingObjective(deviceId,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700367 nextHops,
368 false,
Saurav Das4c35fc42015-11-20 15:27:53 -0800369 true,
370 metabuilder.build());
371 if (fwdObjBosBuilder == null) {
sangho80f11cb2015-04-01 13:05:26 -0700372 return false;
373 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800374 fwdObjBuilders.add(fwdObjBosBuilder);
375
376 // XXX continue case with not-bos - SR app multi label not implemented yet
377 // also requires MPLS ECMP
378 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
379 getMplsForwardingObjective(deviceId,
380 nextHops,
381 false,
382 false); */
383
sangho80f11cb2015-04-01 13:05:26 -0700384 }
385
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700386 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
387 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
388 .makePermanent()).withSelector(selector)
Charles Chanb7f75ac2016-01-11 18:28:54 -0800389 .withPriority(DEFAULT_PRIORITY))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700390 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700391 srManager.flowObjectiveService.
392 forward(deviceId,
393 fwdObjBuilder.
394 add(new SRObjectiveContext(deviceId,
Saurav Das4c35fc42015-11-20 15:27:53 -0800395 SRObjectiveContext.ObjectiveType.FORWARDING)));
sanghofb7c7292015-04-13 15:15:58 -0700396 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700397 }
398
399 return true;
400 }
401
Saurav Das4c35fc42015-11-20 15:27:53 -0800402 private ForwardingObjective.Builder getMplsForwardingObjective(
403 DeviceId deviceId,
404 Set<DeviceId> nextHops,
405 boolean phpRequired,
406 boolean isBos,
407 TrafficSelector meta) {
408
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700409 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
410 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700411
412 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
413
414 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800415 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700416 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700417 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700418 if (isBos) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800419 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType())
420 .decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700421 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800422 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
423 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700424 }
425 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800426 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700427 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700428 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700429 }
430
Saurav Das4c35fc42015-11-20 15:27:53 -0800431 // All forwarding is via ECMP group, the metadata informs the driver
432 // that the next-Objective will be used by MPLS flows. In other words,
433 // MPLS ECMP is requested. It is up to the driver to decide if these
434 // packets will be hashed or not.
435 fwdBuilder.withTreatment(tbuilder.build());
436 NeighborSet ns = new NeighborSet(nextHops);
437 log.debug("Trying to get a nextObjid for mpls rule on device:{} to ns:{}",
438 deviceId, ns);
439
440 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta);
441 if (nextId <= 0) {
442 log.warn("No next objective in {} for ns: {}", deviceId, ns);
443 return null;
sangho80f11cb2015-04-01 13:05:26 -0700444 }
445
Saurav Das4c35fc42015-11-20 15:27:53 -0800446 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700447 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700448 }
449
450 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700451 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700452 * dstMac corresponding to the router's MAC address. For those pipelines
453 * that need to internally assign vlans to untagged packets, this method
454 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700455 * <p>
456 * Note that the vlan assignment is only done by the master-instance for a switch.
457 * However we send the filtering objective from slave-instances as well, so
458 * that drivers can obtain other information (like Router MAC and IP).
sangho80f11cb2015-04-01 13:05:26 -0700459 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700460 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700461 */
Saurav Das9f1c42e2015-10-23 10:51:11 -0700462 public void populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700463 log.debug("Installing per-port filtering objective for untagged "
464 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800465
466 MacAddress deviceMac;
467 try {
468 deviceMac = config.getDeviceMac(deviceId);
469 } catch (DeviceConfigNotFoundException e) {
470 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
471 return;
472 }
473
Saurav Das7c305372015-10-28 12:39:42 -0700474 for (Port port : srManager.deviceService.getPorts(deviceId)) {
475 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
476 Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number());
477 VlanId assignedVlan = (portSubnet == null)
478 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
479 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan319d1a22015-11-03 10:42:14 -0800480
Saurav Das7c305372015-10-28 12:39:42 -0700481 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
482 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan319d1a22015-11-03 10:42:14 -0800483 .addCondition(Criteria.matchEthDst(deviceMac))
Charles Chanb7f75ac2016-01-11 18:28:54 -0800484 .addCondition(Criteria.matchVlanId(VlanId.NONE))
485 .withPriority(DEFAULT_PRIORITY);
Saurav Dasc28b3432015-10-30 17:45:38 -0700486 // vlan assignment is valid only if this instance is master
487 if (srManager.mastershipService.isLocalMaster(deviceId)) {
488 TrafficTreatment tt = DefaultTrafficTreatment.builder()
489 .pushVlan().setVlanId(assignedVlan).build();
Saurav Das2d94d312015-11-24 23:21:05 -0800490 fob.withMeta(tt);
Saurav Dasc28b3432015-10-30 17:45:38 -0700491 }
Saurav Das7c305372015-10-28 12:39:42 -0700492 fob.permit().fromApp(srManager.appId);
493 srManager.flowObjectiveService.
494 filter(deviceId, fob.add(new SRObjectiveContext(deviceId,
495 SRObjectiveContext.ObjectiveType.FILTER)));
496 }
497 }
sangho80f11cb2015-04-01 13:05:26 -0700498 }
499
500 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700501 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700502 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700503 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700504 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700505 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700506 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700507 */
Saurav Das9f1c42e2015-10-23 10:51:11 -0700508 public void populateRouterIpPunts(DeviceId deviceId) {
Charles Chan319d1a22015-11-03 10:42:14 -0800509 Ip4Address routerIp;
510 try {
511 routerIp = config.getRouterIp(deviceId);
512 } catch (DeviceConfigNotFoundException e) {
513 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
514 return;
515 }
516
Saurav Dasc28b3432015-10-30 17:45:38 -0700517 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
518 log.debug("Not installing port-IP punts - not the master for dev:{} ",
519 deviceId);
520 return;
521 }
Saurav Das9f1c42e2015-10-23 10:51:11 -0700522 ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder();
Saurav Dasc28b3432015-10-30 17:45:38 -0700523 Set<Ip4Address> allIps = new HashSet<Ip4Address>(config.getPortIPs(deviceId));
Charles Chan319d1a22015-11-03 10:42:14 -0800524 allIps.add(routerIp);
Saurav Dasc28b3432015-10-30 17:45:38 -0700525 for (Ip4Address ipaddr : allIps) {
Charles Chanf4586112015-11-09 16:37:23 -0800526 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
527 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
528 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
529 sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr,
Saurav Das9f1c42e2015-10-23 10:51:11 -0700530 IpPrefix.MAX_INET_MASK_LENGTH));
Charles Chanf4586112015-11-09 16:37:23 -0800531 tbuilder.setOutput(PortNumber.CONTROLLER);
532 puntIp.withSelector(sbuilder.build());
533 puntIp.withTreatment(tbuilder.build());
Saurav Das9f1c42e2015-10-23 10:51:11 -0700534 puntIp.withFlag(Flag.VERSATILE)
535 .withPriority(HIGHEST_PRIORITY)
536 .makePermanent()
537 .fromApp(srManager.appId);
538 log.debug("Installing forwarding objective to punt port IP addresses");
539 srManager.flowObjectiveService.
540 forward(deviceId,
541 puntIp.add(new SRObjectiveContext(deviceId,
542 SRObjectiveContext.ObjectiveType.FORWARDING)));
543 }
sangho80f11cb2015-04-01 13:05:26 -0700544 }
545
Charles Chanf4586112015-11-09 16:37:23 -0800546 /**
547 * Populates a forwarding objective to send packets that miss other high
548 * priority Bridging Table entries to a group that contains all ports of
549 * its subnet.
550 *
551 * Note: We assume that packets sending from the edge switches to the hosts
552 * have untagged VLAN.
553 * The VLAN tag will be popped later in the flooding group.
554 *
555 * @param deviceId switch ID to set the rules
556 */
557 public void populateSubnetBroadcastRule(DeviceId deviceId) {
558 config.getSubnets(deviceId).forEach(subnet -> {
559 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
560 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
561
Saurav Das2d94d312015-11-24 23:21:05 -0800562 if (nextId < 0 || vlanId == null) {
563 log.error("Cannot install subnet broadcast rule in dev:{} due"
564 + "to vlanId:{} or nextId:{}", vlanId, nextId);
565 return;
566 }
567
Charles Chanf4586112015-11-09 16:37:23 -0800568 /* Driver should treat objective with MacAddress.NONE as the
569 * subnet broadcast rule
570 */
571 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
572 sbuilder.matchVlanId(vlanId);
573 sbuilder.matchEthDst(MacAddress.NONE);
574
575 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
576 fob.withFlag(Flag.SPECIFIC)
577 .withSelector(sbuilder.build())
578 .nextStep(nextId)
Charles Chanb7f75ac2016-01-11 18:28:54 -0800579 .withPriority(FLOOD_PRIORITY)
Charles Chanf4586112015-11-09 16:37:23 -0800580 .fromApp(srManager.appId)
581 .makePermanent();
582
583 srManager.flowObjectiveService.forward(
584 deviceId,
585 fob.add(new SRObjectiveContext(
586 deviceId,
587 SRObjectiveContext.ObjectiveType.FORWARDING)
588 )
589 );
590 });
591 }
592
Charles Chanb7f75ac2016-01-11 18:28:54 -0800593 /**
594 * Creates a filtering objective to permit VLAN cross-connect traffic.
595 *
596 * @param deviceId the DPID of the switch
597 */
598 public void populateXConnectVlanFilters(DeviceId deviceId) {
599 Map<VlanId, List<ConnectPoint>> xConnectsForDevice =
600 config.getXConnects();
601 xConnectsForDevice.forEach((vlanId, connectPoints) -> {
602 // Only proceed the xConnect for given device
603 for (ConnectPoint connectPoint : connectPoints) {
604 if (!connectPoint.deviceId().equals(deviceId)) {
605 return;
606 }
607 }
608
609 connectPoints.forEach(connectPoint -> {
610 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
611 fob.withKey(Criteria.matchInPort(connectPoint.port()))
612 .addCondition(Criteria.matchVlanId(vlanId))
613 .addCondition(Criteria.matchEthDst(MacAddress.NONE))
614 .withPriority(XCONNECT_PRIORITY);
615
616 fob.permit().fromApp(srManager.appId);
617 srManager.flowObjectiveService
618 .filter(deviceId, fob.add(new SRObjectiveContext(deviceId,
619 SRObjectiveContext.ObjectiveType.FILTER)));
620 });
621 });
622 }
623
624 /**
625 * Populates a forwarding objective that points the VLAN cross-connect
626 * packets to a broadcast group.
627 *
628 * @param deviceId switch ID to set the rules
629 */
630 public void populateXConnectBroadcastRule(DeviceId deviceId) {
631 Map<VlanId, List<ConnectPoint>> xConnects =
632 config.getXConnects();
633 xConnects.forEach((vlanId, connectPoints) -> {
634 // Only proceed the xConnect for given device
635 for (ConnectPoint connectPoint : connectPoints) {
636 if (!connectPoint.deviceId().equals(deviceId)) {
637 return;
638 }
639 }
640
641 int nextId = srManager.getXConnectNextObjectiveId(deviceId, vlanId);
642 if (nextId < 0) {
643 log.error("Cannot install cross-connect broadcast rule in dev:{} " +
644 "due to missing nextId:{}", deviceId, nextId);
645 return;
646 }
647
648 /*
649 * Driver should treat objectives with MacAddress.NONE and !VlanId.NONE
650 * as the VLAN cross-connect broadcast rules
651 */
652 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
653 sbuilder.matchVlanId(vlanId);
654 sbuilder.matchEthDst(MacAddress.NONE);
655
656 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
657 fob.withFlag(Flag.SPECIFIC)
658 .withSelector(sbuilder.build())
659 .nextStep(nextId)
660 .withPriority(DEFAULT_PRIORITY)
661 .fromApp(srManager.appId)
662 .makePermanent();
663
664 srManager.flowObjectiveService.forward(
665 deviceId,
666 fob.add(new SRObjectiveContext(
667 deviceId,
668 SRObjectiveContext.ObjectiveType.FORWARDING)
669 )
670 );
671 });
672 }
Charles Chanf4586112015-11-09 16:37:23 -0800673
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700674 private static class SRObjectiveContext implements ObjectiveContext {
675 enum ObjectiveType {
676 FILTER,
677 FORWARDING
678 }
679 final DeviceId deviceId;
680 final ObjectiveType type;
681
682 SRObjectiveContext(DeviceId deviceId, ObjectiveType type) {
683 this.deviceId = deviceId;
684 this.type = type;
685 }
686 @Override
687 public void onSuccess(Objective objective) {
688 log.debug("{} objective operation successful in device {}",
689 type.name(), deviceId);
690 }
691
692 @Override
693 public void onError(Objective objective, ObjectiveError error) {
694 log.warn("{} objective {} operation failed with error: {} in device {}",
695 type.name(), objective, error, deviceId);
696 }
697 }
698
sangho80f11cb2015-04-01 13:05:26 -0700699}