blob: 10ac6d637f86a0dd4c606d80676548889c76cab5 [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;
42import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das9f1c42e2015-10-23 10:51:11 -070043import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sangho80f11cb2015-04-01 13:05:26 -070044import org.slf4j.Logger;
45import org.slf4j.LoggerFactory;
46
47import java.util.ArrayList;
Saurav Dasc28b3432015-10-30 17:45:38 -070048import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070049import java.util.List;
Charles Chanb7f75ac2016-01-11 18:28:54 -080050import java.util.Map;
sangho80f11cb2015-04-01 13:05:26 -070051import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070052import java.util.concurrent.atomic.AtomicLong;
sangho80f11cb2015-04-01 13:05:26 -070053
54import static com.google.common.base.Preconditions.checkNotNull;
55
Charles Chanb7f75ac2016-01-11 18:28:54 -080056/**
57 * Populator of segment routing flow rules.
58 */
sangho80f11cb2015-04-01 13:05:26 -070059public class RoutingRulePopulator {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070060 private static final Logger log = LoggerFactory
61 .getLogger(RoutingRulePopulator.class);
sangho80f11cb2015-04-01 13:05:26 -070062
sanghofb7c7292015-04-13 15:15:58 -070063 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070064 private SegmentRoutingManager srManager;
65 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070066
sangho80f11cb2015-04-01 13:05:26 -070067 /**
68 * Creates a RoutingRulePopulator object.
69 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070070 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070071 */
72 public RoutingRulePopulator(SegmentRoutingManager srManager) {
73 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070074 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-04-13 15:15:58 -070075 this.rulePopulationCounter = new AtomicLong(0);
76 }
77
78 /**
79 * Resets the population counter.
80 */
81 public void resetCounter() {
82 rulePopulationCounter.set(0);
83 }
84
85 /**
86 * Returns the number of rules populated.
Thomas Vachuska7cfc6202015-04-30 18:13:25 -070087 *
88 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -070089 */
90 public long getCounter() {
91 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -070092 }
93
94 /**
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070095 * Populates IP flow rules for specific hosts directly connected to the
96 * switch.
sangho80f11cb2015-04-01 13:05:26 -070097 *
98 * @param deviceId switch ID to set the rules
99 * @param hostIp host IP address
100 * @param hostMac host MAC address
101 * @param outPort port where the host is connected
102 */
103 public void populateIpRuleForHost(DeviceId deviceId, Ip4Address hostIp,
104 MacAddress hostMac, PortNumber outPort) {
Charles Chanf4586112015-11-09 16:37:23 -0800105 log.debug("Populate IP table entry for host {} at {}:{}",
106 hostIp, deviceId, outPort);
107 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800108 try {
Charles Chanf4586112015-11-09 16:37:23 -0800109 fwdBuilder = getForwardingObjectiveBuilder(
110 deviceId, hostIp, hostMac, outPort);
Charles Chan319d1a22015-11-03 10:42:14 -0800111 } catch (DeviceConfigNotFoundException e) {
112 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
113 return;
114 }
Charles Chanf4586112015-11-09 16:37:23 -0800115 srManager.flowObjectiveService.
116 forward(deviceId, fwdBuilder.add(new SRObjectiveContext(deviceId,
117 SRObjectiveContext.ObjectiveType.FORWARDING)));
118 rulePopulationCounter.incrementAndGet();
119 }
120
Charles Chanb7f75ac2016-01-11 18:28:54 -0800121 /**
122 * Removes IP rules for host when the host is gone.
123 *
124 * @param deviceId device ID of the device that host attaches to
125 * @param hostIp IP address of the host
126 * @param hostMac MAC address of the host
127 * @param outPort port that host attaches to
128 */
Charles Chanf4586112015-11-09 16:37:23 -0800129 public void revokeIpRuleForHost(DeviceId deviceId, Ip4Address hostIp,
130 MacAddress hostMac, PortNumber outPort) {
131 log.debug("Revoke IP table entry for host {} at {}:{}",
132 hostIp, deviceId, outPort);
133 ForwardingObjective.Builder fwdBuilder;
134 try {
135 fwdBuilder = getForwardingObjectiveBuilder(
136 deviceId, hostIp, hostMac, outPort);
137 } catch (DeviceConfigNotFoundException e) {
138 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
139 return;
140 }
141 srManager.flowObjectiveService.
142 forward(deviceId, fwdBuilder.remove(new SRObjectiveContext(deviceId,
143 SRObjectiveContext.ObjectiveType.FORWARDING)));
144 }
145
146 private ForwardingObjective.Builder getForwardingObjectiveBuilder(
147 DeviceId deviceId, Ip4Address hostIp,
148 MacAddress hostMac, PortNumber outPort)
149 throws DeviceConfigNotFoundException {
150 MacAddress deviceMac;
151 deviceMac = config.getDeviceMac(deviceId);
Charles Chan82ab1932016-01-30 23:22:37 -0800152 int priority;
Charles Chan319d1a22015-11-03 10:42:14 -0800153
sangho80f11cb2015-04-01 13:05:26 -0700154 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
155 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
156
sangho80f11cb2015-04-01 13:05:26 -0700157 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan82ab1932016-01-30 23:22:37 -0800158 // Special case for default route
159 if (hostIp.isZero()) {
160 sbuilder.matchIPDst(IpPrefix.valueOf(hostIp, 0));
161 priority = SegmentRoutingService.MIN_IP_PRIORITY;
162 } else {
163 Ip4Prefix hostIpPrefix = Ip4Prefix.valueOf(hostIp, IpPrefix.MAX_INET_MASK_LENGTH);
164 sbuilder.matchIPDst(hostIpPrefix);
165 priority = getPriorityFromPrefix(hostIpPrefix);
166 }
Saurav Das2d94d312015-11-24 23:21:05 -0800167 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700168
sangho27462c62015-05-14 00:39:53 -0700169 tbuilder.deferred()
170 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800171 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700172 .setOutput(outPort);
sangho80f11cb2015-04-01 13:05:26 -0700173 TrafficTreatment treatment = tbuilder.build();
Saurav Das2d94d312015-11-24 23:21:05 -0800174
175 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
176 // for switch pipelines that need it, provide outgoing vlan as metadata
177 VlanId outvlan = null;
178 Ip4Prefix subnet = srManager.deviceConfiguration.getPortSubnet(deviceId, outPort);
179 if (subnet == null) {
180 outvlan = VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET);
181 } else {
182 outvlan = srManager.getSubnetAssignedVlanId(deviceId, subnet);
183 }
184 TrafficSelector meta = DefaultTrafficSelector.builder()
185 .matchVlanId(outvlan).build();
186 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
187 treatment, meta);
sangho80f11cb2015-04-01 13:05:26 -0700188
Charles Chanf4586112015-11-09 16:37:23 -0800189 return DefaultForwardingObjective.builder()
Saurav Das2d94d312015-11-24 23:21:05 -0800190 .withSelector(selector)
191 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800192 .fromApp(srManager.appId).makePermanent()
Charles Chan82ab1932016-01-30 23:22:37 -0800193 .withPriority(priority)
194 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700195 }
196
197 /**
198 * Populates IP flow rules for the subnets of the destination router.
199 *
200 * @param deviceId switch ID to set the rules
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700201 * @param subnets subnet information
sangho80f11cb2015-04-01 13:05:26 -0700202 * @param destSw destination switch ID
203 * @param nextHops next hop switch ID list
204 * @return true if all rules are set successfully, false otherwise
205 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700206 public boolean populateIpRuleForSubnet(DeviceId deviceId,
Charles Chanc6ad7752015-10-29 14:58:10 -0700207 Set<Ip4Prefix> subnets,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700208 DeviceId destSw,
209 Set<DeviceId> nextHops) {
sangho80f11cb2015-04-01 13:05:26 -0700210
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700211 for (IpPrefix subnet : subnets) {
sangho80f11cb2015-04-01 13:05:26 -0700212 if (!populateIpRuleForRouter(deviceId, subnet, destSw, nextHops)) {
213 return false;
214 }
215 }
216
217 return true;
218 }
219
220 /**
221 * Populates IP flow rules for the router IP address.
222 *
Saurav Das88979182015-10-19 14:37:36 -0700223 * @param deviceId target device ID to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700224 * @param ipPrefix the IP address of the destination router
225 * @param destSw device ID of the destination router
226 * @param nextHops next hop switch ID list
227 * @return true if all rules are set successfully, false otherwise
228 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700229 public boolean populateIpRuleForRouter(DeviceId deviceId,
230 IpPrefix ipPrefix, DeviceId destSw,
231 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-11-03 10:42:14 -0800232 int segmentId;
233 try {
234 segmentId = config.getSegmentId(destSw);
235 } catch (DeviceConfigNotFoundException e) {
236 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
237 return false;
238 }
sangho80f11cb2015-04-01 13:05:26 -0700239
240 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Charles Chan3ef31e72016-02-05 13:33:54 -0800241 sbuilder.matchIPDst(ipPrefix);
sangho80f11cb2015-04-01 13:05:26 -0700242 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chanf4586112015-11-09 16:37:23 -0800243 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700244
Charles Chanf4586112015-11-09 16:37:23 -0800245 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
246 NeighborSet ns;
247 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700248
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700249 // If the next hop is the same as the final destination, then MPLS label
250 // is not set.
sangho80f11cb2015-04-01 13:05:26 -0700251 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chanf4586112015-11-09 16:37:23 -0800252 tbuilder.immediate().decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700253 ns = new NeighborSet(nextHops);
Charles Chanf4586112015-11-09 16:37:23 -0800254 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700255 } else {
Charles Chan319d1a22015-11-03 10:42:14 -0800256 ns = new NeighborSet(nextHops, segmentId);
Charles Chanf4586112015-11-09 16:37:23 -0800257 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700258 }
259
Saurav Das4c35fc42015-11-20 15:27:53 -0800260 // setup metadata to pass to nextObjective - indicate the vlan on egress
261 // if needed by the switch pipeline. Since neighbor sets are always to
262 // other neighboring routers, there is no subnet assigned on those ports.
263 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
264 metabuilder.matchVlanId(
265 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
266
267 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
268 if (nextId <= 0) {
sangho2165d222015-05-01 09:38:25 -0700269 log.warn("No next objective in {} for ns: {}", deviceId, ns);
270 return false;
271 }
272
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700273 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
274 .builder()
275 .fromApp(srManager.appId)
276 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800277 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700278 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800279 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700280 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800281 if (treatment != null) {
282 fwdBuilder.withTreatment(treatment);
283 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700284 log.debug("Installing IPv4 forwarding objective "
sangho2165d222015-05-01 09:38:25 -0700285 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700286 ipPrefix,
287 deviceId);
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700288 srManager.flowObjectiveService.
289 forward(deviceId,
290 fwdBuilder.
291 add(new SRObjectiveContext(deviceId,
292 SRObjectiveContext.ObjectiveType.FORWARDING)));
sanghofb7c7292015-04-13 15:15:58 -0700293 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700294
295 return true;
296 }
297
sangho80f11cb2015-04-01 13:05:26 -0700298 /**
Saurav Das88979182015-10-19 14:37:36 -0700299 * Populates MPLS flow rules to all routers.
sangho80f11cb2015-04-01 13:05:26 -0700300 *
Saurav Das88979182015-10-19 14:37:36 -0700301 * @param deviceId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700302 * @param destSwId destination switch device ID
303 * @param nextHops next hops switch ID list
304 * @return true if all rules are set successfully, false otherwise
305 */
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700306 public boolean populateMplsRule(DeviceId deviceId, DeviceId destSwId,
307 Set<DeviceId> nextHops) {
Charles Chan319d1a22015-11-03 10:42:14 -0800308 int segmentId;
309 try {
310 segmentId = config.getSegmentId(destSwId);
311 } catch (DeviceConfigNotFoundException e) {
312 log.warn(e.getMessage() + " Aborting populateMplsRule.");
313 return false;
314 }
sangho80f11cb2015-04-01 13:05:26 -0700315
316 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU47b8aa22015-09-11 11:19:11 -0700317 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700318
319 // TODO Handle the case of Bos == false
sangho80f11cb2015-04-01 13:05:26 -0700320 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
Saurav Das4c35fc42015-11-20 15:27:53 -0800321 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
Charles Chande6655c2015-12-23 00:15:11 -0800322 sbuilder.matchMplsBos(true);
Saurav Das4c35fc42015-11-20 15:27:53 -0800323 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700324
Saurav Das4c35fc42015-11-20 15:27:53 -0800325 // setup metadata to pass to nextObjective - indicate the vlan on egress
326 // if needed by the switch pipeline. Since mpls next-hops are always to
327 // other neighboring routers, there is no subnet assigned on those ports.
328 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
329 metabuilder.matchVlanId(
330 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
331
332 // If the next hop is the destination router for the segment, do pop
sangho80f11cb2015-04-01 13:05:26 -0700333 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700334 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das4c35fc42015-11-20 15:27:53 -0800335 + "label {} in switch {} with pop", segmentId, deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700336
Saurav Das4c35fc42015-11-20 15:27:53 -0800337 // bos pop case (php)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700338 ForwardingObjective.Builder fwdObjBosBuilder =
339 getMplsForwardingObjective(deviceId,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700340 nextHops,
341 true,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700342 true,
Saurav Das4c35fc42015-11-20 15:27:53 -0800343 metabuilder.build());
344 if (fwdObjBosBuilder == null) {
sangho80f11cb2015-04-01 13:05:26 -0700345 return false;
346 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800347 fwdObjBuilders.add(fwdObjBosBuilder);
348
349 // XXX not-bos pop case, SR app multi-label not implemented yet
350 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
351 getMplsForwardingObjective(deviceId,
352 nextHops,
353 true,
354 false);*/
355
sangho80f11cb2015-04-01 13:05:26 -0700356 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800357 // next hop is not destination, SR CONTINUE case (swap with self)
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700358 log.debug("Installing MPLS forwarding objective for "
Saurav Das4c35fc42015-11-20 15:27:53 -0800359 + "label {} in switch {} without pop", segmentId, deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700360
Saurav Das4c35fc42015-11-20 15:27:53 -0800361 // continue case with bos - this does get triggered in edge routers
362 // and in core routers - driver can handle depending on availability
363 // of MPLS ECMP or not
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700364 ForwardingObjective.Builder fwdObjBosBuilder =
365 getMplsForwardingObjective(deviceId,
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700366 nextHops,
367 false,
Saurav Das4c35fc42015-11-20 15:27:53 -0800368 true,
369 metabuilder.build());
370 if (fwdObjBosBuilder == null) {
sangho80f11cb2015-04-01 13:05:26 -0700371 return false;
372 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800373 fwdObjBuilders.add(fwdObjBosBuilder);
374
375 // XXX continue case with not-bos - SR app multi label not implemented yet
376 // also requires MPLS ECMP
377 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
378 getMplsForwardingObjective(deviceId,
379 nextHops,
380 false,
381 false); */
382
sangho80f11cb2015-04-01 13:05:26 -0700383 }
384
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700385 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
386 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
387 .makePermanent()).withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800388 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700389 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700390 srManager.flowObjectiveService.
391 forward(deviceId,
392 fwdObjBuilder.
393 add(new SRObjectiveContext(deviceId,
Saurav Das4c35fc42015-11-20 15:27:53 -0800394 SRObjectiveContext.ObjectiveType.FORWARDING)));
sanghofb7c7292015-04-13 15:15:58 -0700395 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700396 }
397
398 return true;
399 }
400
Saurav Das4c35fc42015-11-20 15:27:53 -0800401 private ForwardingObjective.Builder getMplsForwardingObjective(
402 DeviceId deviceId,
403 Set<DeviceId> nextHops,
404 boolean phpRequired,
405 boolean isBos,
406 TrafficSelector meta) {
407
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700408 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
409 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700410
411 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
412
413 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800414 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700415 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700416 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700417 if (isBos) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800418 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType())
419 .decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700420 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800421 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
422 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700423 }
424 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800425 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700426 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700427 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700428 }
429
Saurav Das4c35fc42015-11-20 15:27:53 -0800430 // All forwarding is via ECMP group, the metadata informs the driver
431 // that the next-Objective will be used by MPLS flows. In other words,
432 // MPLS ECMP is requested. It is up to the driver to decide if these
433 // packets will be hashed or not.
434 fwdBuilder.withTreatment(tbuilder.build());
435 NeighborSet ns = new NeighborSet(nextHops);
436 log.debug("Trying to get a nextObjid for mpls rule on device:{} to ns:{}",
437 deviceId, ns);
438
439 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta);
440 if (nextId <= 0) {
441 log.warn("No next objective in {} for ns: {}", deviceId, ns);
442 return null;
sangho80f11cb2015-04-01 13:05:26 -0700443 }
444
Saurav Das4c35fc42015-11-20 15:27:53 -0800445 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700446 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700447 }
448
449 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700450 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700451 * dstMac corresponding to the router's MAC address. For those pipelines
452 * that need to internally assign vlans to untagged packets, this method
453 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700454 * <p>
455 * Note that the vlan assignment is only done by the master-instance for a switch.
456 * However we send the filtering objective from slave-instances as well, so
457 * that drivers can obtain other information (like Router MAC and IP).
sangho80f11cb2015-04-01 13:05:26 -0700458 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700459 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700460 */
Saurav Das9f1c42e2015-10-23 10:51:11 -0700461 public void populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700462 log.debug("Installing per-port filtering objective for untagged "
463 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800464
465 MacAddress deviceMac;
466 try {
467 deviceMac = config.getDeviceMac(deviceId);
468 } catch (DeviceConfigNotFoundException e) {
469 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
470 return;
471 }
472
Saurav Das7c305372015-10-28 12:39:42 -0700473 for (Port port : srManager.deviceService.getPorts(deviceId)) {
Charles Chan82ab1932016-01-30 23:22:37 -0800474 if (port.number().toLong() > 0 &&
475 port.number().toLong() < SegmentRoutingService.OFPP_MAX &&
476 port.isEnabled()) {
Saurav Das7c305372015-10-28 12:39:42 -0700477 Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number());
478 VlanId assignedVlan = (portSubnet == null)
479 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
480 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan319d1a22015-11-03 10:42:14 -0800481
Saurav Das7c305372015-10-28 12:39:42 -0700482 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
483 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan319d1a22015-11-03 10:42:14 -0800484 .addCondition(Criteria.matchEthDst(deviceMac))
Charles Chanb7f75ac2016-01-11 18:28:54 -0800485 .addCondition(Criteria.matchVlanId(VlanId.NONE))
Charles Chan82ab1932016-01-30 23:22:37 -0800486 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Saurav Dasc28b3432015-10-30 17:45:38 -0700487 // vlan assignment is valid only if this instance is master
488 if (srManager.mastershipService.isLocalMaster(deviceId)) {
489 TrafficTreatment tt = DefaultTrafficTreatment.builder()
490 .pushVlan().setVlanId(assignedVlan).build();
Saurav Das2d94d312015-11-24 23:21:05 -0800491 fob.withMeta(tt);
Saurav Dasc28b3432015-10-30 17:45:38 -0700492 }
Saurav Das7c305372015-10-28 12:39:42 -0700493 fob.permit().fromApp(srManager.appId);
494 srManager.flowObjectiveService.
495 filter(deviceId, fob.add(new SRObjectiveContext(deviceId,
496 SRObjectiveContext.ObjectiveType.FILTER)));
497 }
498 }
sangho80f11cb2015-04-01 13:05:26 -0700499 }
500
501 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700502 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700503 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700504 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700505 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700506 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700507 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700508 */
Saurav Das9f1c42e2015-10-23 10:51:11 -0700509 public void populateRouterIpPunts(DeviceId deviceId) {
Charles Chan319d1a22015-11-03 10:42:14 -0800510 Ip4Address routerIp;
511 try {
512 routerIp = config.getRouterIp(deviceId);
513 } catch (DeviceConfigNotFoundException e) {
514 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
515 return;
516 }
517
Saurav Dasc28b3432015-10-30 17:45:38 -0700518 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
519 log.debug("Not installing port-IP punts - not the master for dev:{} ",
520 deviceId);
521 return;
522 }
Saurav Das9f1c42e2015-10-23 10:51:11 -0700523 ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder();
Charles Chan82ab1932016-01-30 23:22:37 -0800524 Set<Ip4Address> allIps = new HashSet<>(config.getPortIPs(deviceId));
Charles Chan319d1a22015-11-03 10:42:14 -0800525 allIps.add(routerIp);
Saurav Dasc28b3432015-10-30 17:45:38 -0700526 for (Ip4Address ipaddr : allIps) {
Charles Chanf4586112015-11-09 16:37:23 -0800527 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
528 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
529 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
530 sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr,
Saurav Das9f1c42e2015-10-23 10:51:11 -0700531 IpPrefix.MAX_INET_MASK_LENGTH));
Charles Chanf4586112015-11-09 16:37:23 -0800532 tbuilder.setOutput(PortNumber.CONTROLLER);
533 puntIp.withSelector(sbuilder.build());
534 puntIp.withTreatment(tbuilder.build());
Saurav Das9f1c42e2015-10-23 10:51:11 -0700535 puntIp.withFlag(Flag.VERSATILE)
Charles Chan82ab1932016-01-30 23:22:37 -0800536 .withPriority(SegmentRoutingService.HIGHEST_PRIORITY)
Saurav Das9f1c42e2015-10-23 10:51:11 -0700537 .makePermanent()
538 .fromApp(srManager.appId);
539 log.debug("Installing forwarding objective to punt port IP addresses");
540 srManager.flowObjectiveService.
541 forward(deviceId,
542 puntIp.add(new SRObjectiveContext(deviceId,
543 SRObjectiveContext.ObjectiveType.FORWARDING)));
544 }
sangho80f11cb2015-04-01 13:05:26 -0700545 }
546
Charles Chanf4586112015-11-09 16:37:23 -0800547 /**
548 * Populates a forwarding objective to send packets that miss other high
549 * priority Bridging Table entries to a group that contains all ports of
550 * its subnet.
551 *
552 * Note: We assume that packets sending from the edge switches to the hosts
553 * have untagged VLAN.
554 * The VLAN tag will be popped later in the flooding group.
555 *
556 * @param deviceId switch ID to set the rules
557 */
558 public void populateSubnetBroadcastRule(DeviceId deviceId) {
559 config.getSubnets(deviceId).forEach(subnet -> {
560 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
561 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
562
Saurav Das2d94d312015-11-24 23:21:05 -0800563 if (nextId < 0 || vlanId == null) {
564 log.error("Cannot install subnet broadcast rule in dev:{} due"
565 + "to vlanId:{} or nextId:{}", vlanId, nextId);
566 return;
567 }
568
Charles Chanf4586112015-11-09 16:37:23 -0800569 /* Driver should treat objective with MacAddress.NONE as the
570 * subnet broadcast rule
571 */
572 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
573 sbuilder.matchVlanId(vlanId);
574 sbuilder.matchEthDst(MacAddress.NONE);
575
576 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
577 fob.withFlag(Flag.SPECIFIC)
578 .withSelector(sbuilder.build())
579 .nextStep(nextId)
Charles Chan82ab1932016-01-30 23:22:37 -0800580 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chanf4586112015-11-09 16:37:23 -0800581 .fromApp(srManager.appId)
582 .makePermanent();
583
584 srManager.flowObjectiveService.forward(
585 deviceId,
586 fob.add(new SRObjectiveContext(
587 deviceId,
588 SRObjectiveContext.ObjectiveType.FORWARDING)
589 )
590 );
591 });
592 }
593
Charles Chanb7f75ac2016-01-11 18:28:54 -0800594 /**
595 * Creates a filtering objective to permit VLAN cross-connect traffic.
596 *
597 * @param deviceId the DPID of the switch
598 */
599 public void populateXConnectVlanFilters(DeviceId deviceId) {
600 Map<VlanId, List<ConnectPoint>> xConnectsForDevice =
601 config.getXConnects();
602 xConnectsForDevice.forEach((vlanId, connectPoints) -> {
603 // Only proceed the xConnect for given device
604 for (ConnectPoint connectPoint : connectPoints) {
605 if (!connectPoint.deviceId().equals(deviceId)) {
606 return;
607 }
608 }
609
610 connectPoints.forEach(connectPoint -> {
611 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
612 fob.withKey(Criteria.matchInPort(connectPoint.port()))
613 .addCondition(Criteria.matchVlanId(vlanId))
614 .addCondition(Criteria.matchEthDst(MacAddress.NONE))
Charles Chan82ab1932016-01-30 23:22:37 -0800615 .withPriority(SegmentRoutingService.XCONNECT_PRIORITY);
Charles Chanb7f75ac2016-01-11 18:28:54 -0800616
617 fob.permit().fromApp(srManager.appId);
618 srManager.flowObjectiveService
619 .filter(deviceId, fob.add(new SRObjectiveContext(deviceId,
620 SRObjectiveContext.ObjectiveType.FILTER)));
621 });
622 });
623 }
624
625 /**
626 * Populates a forwarding objective that points the VLAN cross-connect
627 * packets to a broadcast group.
628 *
629 * @param deviceId switch ID to set the rules
630 */
631 public void populateXConnectBroadcastRule(DeviceId deviceId) {
632 Map<VlanId, List<ConnectPoint>> xConnects =
633 config.getXConnects();
634 xConnects.forEach((vlanId, connectPoints) -> {
635 // Only proceed the xConnect for given device
636 for (ConnectPoint connectPoint : connectPoints) {
637 if (!connectPoint.deviceId().equals(deviceId)) {
638 return;
639 }
640 }
641
642 int nextId = srManager.getXConnectNextObjectiveId(deviceId, vlanId);
643 if (nextId < 0) {
644 log.error("Cannot install cross-connect broadcast rule in dev:{} " +
645 "due to missing nextId:{}", deviceId, nextId);
646 return;
647 }
648
649 /*
650 * Driver should treat objectives with MacAddress.NONE and !VlanId.NONE
651 * as the VLAN cross-connect broadcast rules
652 */
653 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
654 sbuilder.matchVlanId(vlanId);
655 sbuilder.matchEthDst(MacAddress.NONE);
656
657 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
658 fob.withFlag(Flag.SPECIFIC)
659 .withSelector(sbuilder.build())
660 .nextStep(nextId)
Charles Chan82ab1932016-01-30 23:22:37 -0800661 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY)
Charles Chanb7f75ac2016-01-11 18:28:54 -0800662 .fromApp(srManager.appId)
663 .makePermanent();
664
665 srManager.flowObjectiveService.forward(
666 deviceId,
667 fob.add(new SRObjectiveContext(
668 deviceId,
669 SRObjectiveContext.ObjectiveType.FORWARDING)
670 )
671 );
672 });
673 }
Charles Chanf4586112015-11-09 16:37:23 -0800674
Charles Chan82ab1932016-01-30 23:22:37 -0800675 private int getPriorityFromPrefix(IpPrefix prefix) {
676 return (prefix.isIp4()) ?
677 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
678 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -0700679 }
sangho80f11cb2015-04-01 13:05:26 -0700680}