blob: 1fae85867da0b4a2bf754381acadf258131d4c34 [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 Chan0b4e6182015-11-03 10:42:14 -080027import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
28import org.onosproject.segmentrouting.config.DeviceConfiguration;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070029import org.onosproject.segmentrouting.grouphandler.NeighborSet;
sanghob35a6192015-04-01 13:05:26 -070030import org.onosproject.net.DeviceId;
Saurav Das0e99e2b2015-10-28 12:39:42 -070031import org.onosproject.net.Port;
sanghob35a6192015-04-01 13:05:26 -070032import org.onosproject.net.PortNumber;
sanghob35a6192015-04-01 13:05:26 -070033import org.onosproject.net.flow.DefaultTrafficSelector;
34import org.onosproject.net.flow.DefaultTrafficTreatment;
sanghob35a6192015-04-01 13:05:26 -070035import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-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 Das822c4e22015-10-23 10:51:11 -070043import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sanghob35a6192015-04-01 13:05:26 -070044import org.slf4j.Logger;
45import org.slf4j.LoggerFactory;
46
47import java.util.ArrayList;
Saurav Das837e0bb2015-10-30 17:45:38 -070048import java.util.HashSet;
sanghob35a6192015-04-01 13:05:26 -070049import java.util.List;
Charles Chane849c192016-01-11 18:28:54 -080050import java.util.Map;
sanghob35a6192015-04-01 13:05:26 -070051import java.util.Set;
sangho20eff1d2015-04-13 15:15:58 -070052import java.util.concurrent.atomic.AtomicLong;
sanghob35a6192015-04-01 13:05:26 -070053
54import static com.google.common.base.Preconditions.checkNotNull;
55
Charles Chane849c192016-01-11 18:28:54 -080056/**
57 * Populator of segment routing flow rules.
58 */
sanghob35a6192015-04-01 13:05:26 -070059public class RoutingRulePopulator {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070060 private static final Logger log = LoggerFactory
61 .getLogger(RoutingRulePopulator.class);
sanghob35a6192015-04-01 13:05:26 -070062
sangho20eff1d2015-04-13 15:15:58 -070063 private AtomicLong rulePopulationCounter;
sangho666cd6d2015-04-14 16:27:13 -070064 private SegmentRoutingManager srManager;
65 private DeviceConfiguration config;
Saurav Das822c4e22015-10-23 10:51:11 -070066
sanghob35a6192015-04-01 13:05:26 -070067 /**
68 * Creates a RoutingRulePopulator object.
69 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -070070 * @param srManager segment routing manager reference
sanghob35a6192015-04-01 13:05:26 -070071 */
72 public RoutingRulePopulator(SegmentRoutingManager srManager) {
73 this.srManager = srManager;
sangho666cd6d2015-04-14 16:27:13 -070074 this.config = checkNotNull(srManager.deviceConfiguration);
sangho20eff1d2015-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 Vachuska266b4432015-04-30 18:13:25 -070087 *
88 * @return number of rules
sangho20eff1d2015-04-13 15:15:58 -070089 */
90 public long getCounter() {
91 return rulePopulationCounter.get();
sanghob35a6192015-04-01 13:05:26 -070092 }
93
94 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070095 * Populates IP flow rules for specific hosts directly connected to the
96 * switch.
sanghob35a6192015-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 Chan68aa62d2015-11-09 16:37:23 -0800105 log.debug("Populate IP table entry for host {} at {}:{}",
106 hostIp, deviceId, outPort);
107 ForwardingObjective.Builder fwdBuilder;
Charles Chan0b4e6182015-11-03 10:42:14 -0800108 try {
Charles Chan68aa62d2015-11-09 16:37:23 -0800109 fwdBuilder = getForwardingObjectiveBuilder(
110 deviceId, hostIp, hostMac, outPort);
Charles Chan0b4e6182015-11-03 10:42:14 -0800111 } catch (DeviceConfigNotFoundException e) {
112 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
113 return;
114 }
Charles Chan68aa62d2015-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 Chane849c192016-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 Chan68aa62d2015-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 Chan5270ed02016-01-30 23:22:37 -0800152 int priority;
Charles Chan0b4e6182015-11-03 10:42:14 -0800153
sanghob35a6192015-04-01 13:05:26 -0700154 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
155 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
156
sanghob35a6192015-04-01 13:05:26 -0700157 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan5270ed02016-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 Das4ce45962015-11-24 23:21:05 -0800167 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700168
sangho1e575652015-05-14 00:39:53 -0700169 tbuilder.deferred()
170 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800171 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700172 .setOutput(outPort);
sanghob35a6192015-04-01 13:05:26 -0700173 TrafficTreatment treatment = tbuilder.build();
Saurav Das4ce45962015-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);
sanghob35a6192015-04-01 13:05:26 -0700188
Charles Chan68aa62d2015-11-09 16:37:23 -0800189 return DefaultForwardingObjective.builder()
Saurav Das4ce45962015-11-24 23:21:05 -0800190 .withSelector(selector)
191 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800192 .fromApp(srManager.appId).makePermanent()
Charles Chan5270ed02016-01-30 23:22:37 -0800193 .withPriority(priority)
194 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-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 Vavilapalli4db76e32015-04-07 15:12:32 -0700201 * @param subnets subnet information
sanghob35a6192015-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 Vavilapallif5b234a2015-04-21 13:04:13 -0700206 public boolean populateIpRuleForSubnet(DeviceId deviceId,
Charles Chan9f676b62015-10-29 14:58:10 -0700207 Set<Ip4Prefix> subnets,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700208 DeviceId destSw,
209 Set<DeviceId> nextHops) {
sanghob35a6192015-04-01 13:05:26 -0700210
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700211 for (IpPrefix subnet : subnets) {
sanghob35a6192015-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 Dasa07f2032015-10-19 14:37:36 -0700223 * @param deviceId target device ID to set the rules
sanghob35a6192015-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 Vavilapallif5b234a2015-04-21 13:04:13 -0700229 public boolean populateIpRuleForRouter(DeviceId deviceId,
230 IpPrefix ipPrefix, DeviceId destSw,
231 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-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 }
sanghob35a6192015-04-01 13:05:26 -0700239
240 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Charles Chan17d38f42016-02-05 13:33:54 -0800241 sbuilder.matchIPDst(ipPrefix);
sanghob35a6192015-04-01 13:05:26 -0700242 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan68aa62d2015-11-09 16:37:23 -0800243 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700244
Charles Chan68aa62d2015-11-09 16:37:23 -0800245 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
246 NeighborSet ns;
247 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700248
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700249 // If the next hop is the same as the final destination, then MPLS label
250 // is not set.
sanghob35a6192015-04-01 13:05:26 -0700251 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800252 tbuilder.immediate().decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700253 ns = new NeighborSet(nextHops);
Charles Chan68aa62d2015-11-09 16:37:23 -0800254 treatment = tbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700255 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800256 ns = new NeighborSet(nextHops, segmentId);
Charles Chan68aa62d2015-11-09 16:37:23 -0800257 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700258 }
259
Saurav Das8a0732e2015-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) {
sangho834e4b02015-05-01 09:38:25 -0700269 log.warn("No next objective in {} for ns: {}", deviceId, ns);
270 return false;
271 }
272
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700273 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
274 .builder()
275 .fromApp(srManager.appId)
276 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800277 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700278 .withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800279 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700280 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800281 if (treatment != null) {
282 fwdBuilder.withTreatment(treatment);
283 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700284 log.debug("Installing IPv4 forwarding objective "
sangho834e4b02015-05-01 09:38:25 -0700285 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700286 ipPrefix,
287 deviceId);
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700288 srManager.flowObjectiveService.
289 forward(deviceId,
290 fwdBuilder.
291 add(new SRObjectiveContext(deviceId,
292 SRObjectiveContext.ObjectiveType.FORWARDING)));
sangho20eff1d2015-04-13 15:15:58 -0700293 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700294
295 return true;
296 }
297
sanghob35a6192015-04-01 13:05:26 -0700298 /**
Saurav Dasa07f2032015-10-19 14:37:36 -0700299 * Populates MPLS flow rules to all routers.
sanghob35a6192015-04-01 13:05:26 -0700300 *
Saurav Dasa07f2032015-10-19 14:37:36 -0700301 * @param deviceId target device ID of the switch to set the rules
sanghob35a6192015-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 Vavilapallif5b234a2015-04-21 13:04:13 -0700306 public boolean populateMplsRule(DeviceId deviceId, DeviceId destSwId,
307 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-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 }
sanghob35a6192015-04-01 13:05:26 -0700315
316 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700317 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700318
319 // TODO Handle the case of Bos == false
sanghob35a6192015-04-01 13:05:26 -0700320 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
Saurav Das8a0732e2015-11-20 15:27:53 -0800321 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
Charles Chan188ebf52015-12-23 00:15:11 -0800322 sbuilder.matchMplsBos(true);
Saurav Das8a0732e2015-11-20 15:27:53 -0800323 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700324
Saurav Das8a0732e2015-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
sanghob35a6192015-04-01 13:05:26 -0700333 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700334 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800335 + "label {} in switch {} with pop", segmentId, deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700336
Saurav Das8a0732e2015-11-20 15:27:53 -0800337 // bos pop case (php)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700338 ForwardingObjective.Builder fwdObjBosBuilder =
339 getMplsForwardingObjective(deviceId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700340 nextHops,
341 true,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700342 true,
Saurav Das8a0732e2015-11-20 15:27:53 -0800343 metabuilder.build());
344 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700345 return false;
346 }
Saurav Das8a0732e2015-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
sanghob35a6192015-04-01 13:05:26 -0700356 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800357 // next hop is not destination, SR CONTINUE case (swap with self)
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700358 log.debug("Installing MPLS forwarding objective for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800359 + "label {} in switch {} without pop", segmentId, deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700360
Saurav Das8a0732e2015-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 Vavilapallif5b234a2015-04-21 13:04:13 -0700364 ForwardingObjective.Builder fwdObjBosBuilder =
365 getMplsForwardingObjective(deviceId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700366 nextHops,
367 false,
Saurav Das8a0732e2015-11-20 15:27:53 -0800368 true,
369 metabuilder.build());
370 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700371 return false;
372 }
Saurav Das8a0732e2015-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
sanghob35a6192015-04-01 13:05:26 -0700383 }
384
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700385 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
386 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
387 .makePermanent()).withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800388 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700389 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700390 srManager.flowObjectiveService.
391 forward(deviceId,
392 fwdObjBuilder.
393 add(new SRObjectiveContext(deviceId,
Saurav Das8a0732e2015-11-20 15:27:53 -0800394 SRObjectiveContext.ObjectiveType.FORWARDING)));
sangho20eff1d2015-04-13 15:15:58 -0700395 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700396 }
397
398 return true;
399 }
400
Saurav Das8a0732e2015-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 Vavilapallif5b234a2015-04-21 13:04:13 -0700408 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
409 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700410
411 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
412
413 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800414 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700415 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700416 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700417 if (isBos) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800418 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType())
419 .decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700420 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800421 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
422 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700423 }
424 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800425 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700426 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700427 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700428 }
429
Saurav Das8a0732e2015-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;
sanghob35a6192015-04-01 13:05:26 -0700443 }
444
Saurav Das8a0732e2015-11-20 15:27:53 -0800445 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700446 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700447 }
448
449 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700450 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-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 Das837e0bb2015-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).
sanghob35a6192015-04-01 13:05:26 -0700458 *
Saurav Das822c4e22015-10-23 10:51:11 -0700459 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700460 */
Saurav Das822c4e22015-10-23 10:51:11 -0700461 public void populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700462 log.debug("Installing per-port filtering objective for untagged "
463 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-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 Das0e99e2b2015-10-28 12:39:42 -0700473 for (Port port : srManager.deviceService.getPorts(deviceId)) {
Charles Chanf2565a92016-02-10 20:46:58 -0800474 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
475 // TODO: Handles dynamic port events when we are ready for dynamic config
Charles Chand9681e72016-02-22 19:27:29 -0800476 if (!srManager.deviceConfiguration.suppressSubnet().contains(cp) &&
Charles Chan5270ed02016-01-30 23:22:37 -0800477 port.isEnabled()) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700478 Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number());
479 VlanId assignedVlan = (portSubnet == null)
480 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
481 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan0b4e6182015-11-03 10:42:14 -0800482
Saurav Das0e99e2b2015-10-28 12:39:42 -0700483 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
484 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan0b4e6182015-11-03 10:42:14 -0800485 .addCondition(Criteria.matchEthDst(deviceMac))
Charles Chane849c192016-01-11 18:28:54 -0800486 .addCondition(Criteria.matchVlanId(VlanId.NONE))
Charles Chan5270ed02016-01-30 23:22:37 -0800487 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Saurav Das837e0bb2015-10-30 17:45:38 -0700488 // vlan assignment is valid only if this instance is master
489 if (srManager.mastershipService.isLocalMaster(deviceId)) {
490 TrafficTreatment tt = DefaultTrafficTreatment.builder()
491 .pushVlan().setVlanId(assignedVlan).build();
Saurav Das4ce45962015-11-24 23:21:05 -0800492 fob.withMeta(tt);
Saurav Das837e0bb2015-10-30 17:45:38 -0700493 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700494 fob.permit().fromApp(srManager.appId);
495 srManager.flowObjectiveService.
496 filter(deviceId, fob.add(new SRObjectiveContext(deviceId,
497 SRObjectiveContext.ObjectiveType.FILTER)));
498 }
499 }
sanghob35a6192015-04-01 13:05:26 -0700500 }
501
502 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700503 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700504 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700505 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700506 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700507 *
Saurav Das822c4e22015-10-23 10:51:11 -0700508 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700509 */
Saurav Das822c4e22015-10-23 10:51:11 -0700510 public void populateRouterIpPunts(DeviceId deviceId) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800511 Ip4Address routerIp;
512 try {
513 routerIp = config.getRouterIp(deviceId);
514 } catch (DeviceConfigNotFoundException e) {
515 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
516 return;
517 }
518
Saurav Das837e0bb2015-10-30 17:45:38 -0700519 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
520 log.debug("Not installing port-IP punts - not the master for dev:{} ",
521 deviceId);
522 return;
523 }
Saurav Das822c4e22015-10-23 10:51:11 -0700524 ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder();
Charles Chan5270ed02016-01-30 23:22:37 -0800525 Set<Ip4Address> allIps = new HashSet<>(config.getPortIPs(deviceId));
Charles Chan0b4e6182015-11-03 10:42:14 -0800526 allIps.add(routerIp);
Saurav Das837e0bb2015-10-30 17:45:38 -0700527 for (Ip4Address ipaddr : allIps) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800528 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
529 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
530 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
531 sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr,
Saurav Das822c4e22015-10-23 10:51:11 -0700532 IpPrefix.MAX_INET_MASK_LENGTH));
Charles Chan68aa62d2015-11-09 16:37:23 -0800533 tbuilder.setOutput(PortNumber.CONTROLLER);
534 puntIp.withSelector(sbuilder.build());
535 puntIp.withTreatment(tbuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700536 puntIp.withFlag(Flag.VERSATILE)
Charles Chan5270ed02016-01-30 23:22:37 -0800537 .withPriority(SegmentRoutingService.HIGHEST_PRIORITY)
Saurav Das822c4e22015-10-23 10:51:11 -0700538 .makePermanent()
539 .fromApp(srManager.appId);
540 log.debug("Installing forwarding objective to punt port IP addresses");
541 srManager.flowObjectiveService.
542 forward(deviceId,
543 puntIp.add(new SRObjectiveContext(deviceId,
544 SRObjectiveContext.ObjectiveType.FORWARDING)));
545 }
sanghob35a6192015-04-01 13:05:26 -0700546 }
547
Charles Chan68aa62d2015-11-09 16:37:23 -0800548 /**
549 * Populates a forwarding objective to send packets that miss other high
550 * priority Bridging Table entries to a group that contains all ports of
551 * its subnet.
552 *
553 * Note: We assume that packets sending from the edge switches to the hosts
554 * have untagged VLAN.
555 * The VLAN tag will be popped later in the flooding group.
556 *
557 * @param deviceId switch ID to set the rules
558 */
559 public void populateSubnetBroadcastRule(DeviceId deviceId) {
560 config.getSubnets(deviceId).forEach(subnet -> {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800561 if (subnet.prefixLength() == 0 ||
562 subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) {
563 return;
564 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800565 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
566 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
567
Saurav Das4ce45962015-11-24 23:21:05 -0800568 if (nextId < 0 || vlanId == null) {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800569 log.error("Cannot install subnet {} broadcast rule in dev:{} due"
570 + "to vlanId:{} or nextId:{}", subnet, deviceId, vlanId, nextId);
Saurav Das4ce45962015-11-24 23:21:05 -0800571 return;
572 }
573
Charles Chan68aa62d2015-11-09 16:37:23 -0800574 /* Driver should treat objective with MacAddress.NONE as the
575 * subnet broadcast rule
576 */
577 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
578 sbuilder.matchVlanId(vlanId);
579 sbuilder.matchEthDst(MacAddress.NONE);
580
581 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
582 fob.withFlag(Flag.SPECIFIC)
583 .withSelector(sbuilder.build())
584 .nextStep(nextId)
Charles Chan5270ed02016-01-30 23:22:37 -0800585 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chan68aa62d2015-11-09 16:37:23 -0800586 .fromApp(srManager.appId)
587 .makePermanent();
588
589 srManager.flowObjectiveService.forward(
590 deviceId,
591 fob.add(new SRObjectiveContext(
592 deviceId,
593 SRObjectiveContext.ObjectiveType.FORWARDING)
594 )
595 );
596 });
597 }
598
Charles Chane849c192016-01-11 18:28:54 -0800599 /**
600 * Creates a filtering objective to permit VLAN cross-connect traffic.
601 *
602 * @param deviceId the DPID of the switch
603 */
604 public void populateXConnectVlanFilters(DeviceId deviceId) {
605 Map<VlanId, List<ConnectPoint>> xConnectsForDevice =
606 config.getXConnects();
607 xConnectsForDevice.forEach((vlanId, connectPoints) -> {
608 // Only proceed the xConnect for given device
609 for (ConnectPoint connectPoint : connectPoints) {
610 if (!connectPoint.deviceId().equals(deviceId)) {
611 return;
612 }
613 }
614
615 connectPoints.forEach(connectPoint -> {
616 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
617 fob.withKey(Criteria.matchInPort(connectPoint.port()))
618 .addCondition(Criteria.matchVlanId(vlanId))
619 .addCondition(Criteria.matchEthDst(MacAddress.NONE))
Charles Chan5270ed02016-01-30 23:22:37 -0800620 .withPriority(SegmentRoutingService.XCONNECT_PRIORITY);
Charles Chane849c192016-01-11 18:28:54 -0800621
622 fob.permit().fromApp(srManager.appId);
623 srManager.flowObjectiveService
624 .filter(deviceId, fob.add(new SRObjectiveContext(deviceId,
625 SRObjectiveContext.ObjectiveType.FILTER)));
626 });
627 });
628 }
629
630 /**
631 * Populates a forwarding objective that points the VLAN cross-connect
632 * packets to a broadcast group.
633 *
634 * @param deviceId switch ID to set the rules
635 */
636 public void populateXConnectBroadcastRule(DeviceId deviceId) {
637 Map<VlanId, List<ConnectPoint>> xConnects =
638 config.getXConnects();
639 xConnects.forEach((vlanId, connectPoints) -> {
640 // Only proceed the xConnect for given device
641 for (ConnectPoint connectPoint : connectPoints) {
642 if (!connectPoint.deviceId().equals(deviceId)) {
643 return;
644 }
645 }
646
647 int nextId = srManager.getXConnectNextObjectiveId(deviceId, vlanId);
648 if (nextId < 0) {
649 log.error("Cannot install cross-connect broadcast rule in dev:{} " +
650 "due to missing nextId:{}", deviceId, nextId);
651 return;
652 }
653
654 /*
655 * Driver should treat objectives with MacAddress.NONE and !VlanId.NONE
656 * as the VLAN cross-connect broadcast rules
657 */
658 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
659 sbuilder.matchVlanId(vlanId);
660 sbuilder.matchEthDst(MacAddress.NONE);
661
662 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
663 fob.withFlag(Flag.SPECIFIC)
664 .withSelector(sbuilder.build())
665 .nextStep(nextId)
Charles Chan5270ed02016-01-30 23:22:37 -0800666 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY)
Charles Chane849c192016-01-11 18:28:54 -0800667 .fromApp(srManager.appId)
668 .makePermanent();
669
670 srManager.flowObjectiveService.forward(
671 deviceId,
672 fob.add(new SRObjectiveContext(
673 deviceId,
674 SRObjectiveContext.ObjectiveType.FORWARDING)
675 )
676 );
677 });
678 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800679
Charles Chan5270ed02016-01-30 23:22:37 -0800680 private int getPriorityFromPrefix(IpPrefix prefix) {
681 return (prefix.isIp4()) ?
682 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
683 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700684 }
sanghob35a6192015-04-01 13:05:26 -0700685}