blob: 8ca29b0df141d45b860867b29b13b35f89ad682c [file] [log] [blame]
sanghob35a6192015-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 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;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -070042import org.onosproject.net.flowobjective.Objective;
43import org.onosproject.net.flowobjective.ObjectiveError;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070044import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das822c4e22015-10-23 10:51:11 -070045import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -070046import org.onosproject.net.flowobjective.ObjectiveContext;
sanghob35a6192015-04-01 13:05:26 -070047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
50import java.util.ArrayList;
Saurav Das837e0bb2015-10-30 17:45:38 -070051import java.util.HashSet;
sanghob35a6192015-04-01 13:05:26 -070052import java.util.List;
Charles Chane849c192016-01-11 18:28:54 -080053import java.util.Map;
sanghob35a6192015-04-01 13:05:26 -070054import java.util.Set;
sangho20eff1d2015-04-13 15:15:58 -070055import java.util.concurrent.atomic.AtomicLong;
sanghob35a6192015-04-01 13:05:26 -070056
57import static com.google.common.base.Preconditions.checkNotNull;
58
Charles Chane849c192016-01-11 18:28:54 -080059/**
60 * Populator of segment routing flow rules.
61 */
sanghob35a6192015-04-01 13:05:26 -070062public class RoutingRulePopulator {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070063 private static final Logger log = LoggerFactory
64 .getLogger(RoutingRulePopulator.class);
sanghob35a6192015-04-01 13:05:26 -070065
sangho20eff1d2015-04-13 15:15:58 -070066 private AtomicLong rulePopulationCounter;
sangho666cd6d2015-04-14 16:27:13 -070067 private SegmentRoutingManager srManager;
68 private DeviceConfiguration config;
Saurav Das822c4e22015-10-23 10:51:11 -070069
70 private static final int HIGHEST_PRIORITY = 0xffff;
Charles Chane849c192016-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 Das0e99e2b2015-10-28 12:39:42 -070075 private static final long OFPP_MAX = 0xffffff00L;
76
Saurav Das822c4e22015-10-23 10:51:11 -070077
sanghob35a6192015-04-01 13:05:26 -070078 /**
79 * Creates a RoutingRulePopulator object.
80 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -070081 * @param srManager segment routing manager reference
sanghob35a6192015-04-01 13:05:26 -070082 */
83 public RoutingRulePopulator(SegmentRoutingManager srManager) {
84 this.srManager = srManager;
sangho666cd6d2015-04-14 16:27:13 -070085 this.config = checkNotNull(srManager.deviceConfiguration);
sangho20eff1d2015-04-13 15:15:58 -070086 this.rulePopulationCounter = new AtomicLong(0);
87 }
88
89 /**
90 * Resets the population counter.
91 */
92 public void resetCounter() {
93 rulePopulationCounter.set(0);
94 }
95
96 /**
97 * Returns the number of rules populated.
Thomas Vachuska266b4432015-04-30 18:13:25 -070098 *
99 * @return number of rules
sangho20eff1d2015-04-13 15:15:58 -0700100 */
101 public long getCounter() {
102 return rulePopulationCounter.get();
sanghob35a6192015-04-01 13:05:26 -0700103 }
104
105 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700106 * Populates IP flow rules for specific hosts directly connected to the
107 * switch.
sanghob35a6192015-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 Chan68aa62d2015-11-09 16:37:23 -0800116 log.debug("Populate IP table entry for host {} at {}:{}",
117 hostIp, deviceId, outPort);
118 ForwardingObjective.Builder fwdBuilder;
Charles Chan0b4e6182015-11-03 10:42:14 -0800119 try {
Charles Chan68aa62d2015-11-09 16:37:23 -0800120 fwdBuilder = getForwardingObjectiveBuilder(
121 deviceId, hostIp, hostMac, outPort);
Charles Chan0b4e6182015-11-03 10:42:14 -0800122 } catch (DeviceConfigNotFoundException e) {
123 log.warn(e.getMessage() + " Aborting populateIpRuleForHost.");
124 return;
125 }
Charles Chan68aa62d2015-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 Chane849c192016-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 Chan68aa62d2015-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 Chan0b4e6182015-11-03 10:42:14 -0800163
sanghob35a6192015-04-01 13:05:26 -0700164 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
165 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
166
sanghob35a6192015-04-01 13:05:26 -0700167 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Saurav Das4ce45962015-11-24 23:21:05 -0800168 sbuilder.matchIPDst(IpPrefix.valueOf(hostIp, IpPrefix.MAX_INET_MASK_LENGTH));
169 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700170
sangho1e575652015-05-14 00:39:53 -0700171 tbuilder.deferred()
172 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800173 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700174 .setOutput(outPort);
sanghob35a6192015-04-01 13:05:26 -0700175 TrafficTreatment treatment = tbuilder.build();
Saurav Das4ce45962015-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);
sanghob35a6192015-04-01 13:05:26 -0700190
Charles Chan68aa62d2015-11-09 16:37:23 -0800191 return DefaultForwardingObjective.builder()
Saurav Das4ce45962015-11-24 23:21:05 -0800192 .withSelector(selector)
193 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800194 .fromApp(srManager.appId).makePermanent()
Charles Chane849c192016-01-11 18:28:54 -0800195 .withPriority(DEFAULT_PRIORITY).withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-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 Vavilapalli4db76e32015-04-07 15:12:32 -0700202 * @param subnets subnet information
sanghob35a6192015-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 Vavilapallif5b234a2015-04-21 13:04:13 -0700207 public boolean populateIpRuleForSubnet(DeviceId deviceId,
Charles Chan9f676b62015-10-29 14:58:10 -0700208 Set<Ip4Prefix> subnets,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700209 DeviceId destSw,
210 Set<DeviceId> nextHops) {
sanghob35a6192015-04-01 13:05:26 -0700211
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700212 for (IpPrefix subnet : subnets) {
sanghob35a6192015-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 Dasa07f2032015-10-19 14:37:36 -0700224 * @param deviceId target device ID to set the rules
sanghob35a6192015-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 Vavilapallif5b234a2015-04-21 13:04:13 -0700230 public boolean populateIpRuleForRouter(DeviceId deviceId,
231 IpPrefix ipPrefix, DeviceId destSw,
232 Set<DeviceId> nextHops) {
Charles Chan14967c22015-12-07 11:11:50 -0800233 // TODO: OFDPA does not support /32 with ECMP group at this moment.
234 // Use /31 instead
235 IpPrefix effectivePrefix =
236 (ipPrefix.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) ?
237 IpPrefix.valueOf(ipPrefix.getIp4Prefix().address(), 31) :
238 ipPrefix;
239
Charles Chan0b4e6182015-11-03 10:42:14 -0800240 int segmentId;
241 try {
242 segmentId = config.getSegmentId(destSw);
243 } catch (DeviceConfigNotFoundException e) {
244 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
245 return false;
246 }
sanghob35a6192015-04-01 13:05:26 -0700247
248 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800249 sbuilder.matchIPDst(effectivePrefix);
sanghob35a6192015-04-01 13:05:26 -0700250 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan68aa62d2015-11-09 16:37:23 -0800251 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700252
Charles Chan68aa62d2015-11-09 16:37:23 -0800253 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
254 NeighborSet ns;
255 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700256
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700257 // If the next hop is the same as the final destination, then MPLS label
258 // is not set.
sanghob35a6192015-04-01 13:05:26 -0700259 if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800260 tbuilder.immediate().decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700261 ns = new NeighborSet(nextHops);
Charles Chan68aa62d2015-11-09 16:37:23 -0800262 treatment = tbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700263 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800264 ns = new NeighborSet(nextHops, segmentId);
Charles Chan68aa62d2015-11-09 16:37:23 -0800265 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700266 }
267
Saurav Das8a0732e2015-11-20 15:27:53 -0800268 // setup metadata to pass to nextObjective - indicate the vlan on egress
269 // if needed by the switch pipeline. Since neighbor sets are always to
270 // other neighboring routers, there is no subnet assigned on those ports.
271 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
272 metabuilder.matchVlanId(
273 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
274
275 int nextId = srManager.getNextObjectiveId(deviceId, ns, metabuilder.build());
276 if (nextId <= 0) {
sangho834e4b02015-05-01 09:38:25 -0700277 log.warn("No next objective in {} for ns: {}", deviceId, ns);
278 return false;
279 }
280
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700281 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
282 .builder()
283 .fromApp(srManager.appId)
284 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800285 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700286 .withSelector(selector)
Charles Chan14967c22015-12-07 11:11:50 -0800287 .withPriority(2000 * effectivePrefix.prefixLength())
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700288 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800289 if (treatment != null) {
290 fwdBuilder.withTreatment(treatment);
291 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700292 log.debug("Installing IPv4 forwarding objective "
sangho834e4b02015-05-01 09:38:25 -0700293 + "for router IP/subnet {} in switch {}",
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700294 ipPrefix,
295 deviceId);
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700296 srManager.flowObjectiveService.
297 forward(deviceId,
298 fwdBuilder.
299 add(new SRObjectiveContext(deviceId,
300 SRObjectiveContext.ObjectiveType.FORWARDING)));
sangho20eff1d2015-04-13 15:15:58 -0700301 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700302
303 return true;
304 }
305
sanghob35a6192015-04-01 13:05:26 -0700306 /**
Saurav Dasa07f2032015-10-19 14:37:36 -0700307 * Populates MPLS flow rules to all routers.
sanghob35a6192015-04-01 13:05:26 -0700308 *
Saurav Dasa07f2032015-10-19 14:37:36 -0700309 * @param deviceId target device ID of the switch to set the rules
sanghob35a6192015-04-01 13:05:26 -0700310 * @param destSwId destination switch device ID
311 * @param nextHops next hops switch ID list
312 * @return true if all rules are set successfully, false otherwise
313 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700314 public boolean populateMplsRule(DeviceId deviceId, DeviceId destSwId,
315 Set<DeviceId> nextHops) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800316 int segmentId;
317 try {
318 segmentId = config.getSegmentId(destSwId);
319 } catch (DeviceConfigNotFoundException e) {
320 log.warn(e.getMessage() + " Aborting populateMplsRule.");
321 return false;
322 }
sanghob35a6192015-04-01 13:05:26 -0700323
324 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700325 List<ForwardingObjective.Builder> fwdObjBuilders = new ArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700326
327 // TODO Handle the case of Bos == false
sanghob35a6192015-04-01 13:05:26 -0700328 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
Saurav Das8a0732e2015-11-20 15:27:53 -0800329 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
Charles Chan188ebf52015-12-23 00:15:11 -0800330 sbuilder.matchMplsBos(true);
Saurav Das8a0732e2015-11-20 15:27:53 -0800331 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700332
Saurav Das8a0732e2015-11-20 15:27:53 -0800333 // setup metadata to pass to nextObjective - indicate the vlan on egress
334 // if needed by the switch pipeline. Since mpls next-hops are always to
335 // other neighboring routers, there is no subnet assigned on those ports.
336 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
337 metabuilder.matchVlanId(
338 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
339
340 // If the next hop is the destination router for the segment, do pop
sanghob35a6192015-04-01 13:05:26 -0700341 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700342 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800343 + "label {} in switch {} with pop", segmentId, deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700344
Saurav Das8a0732e2015-11-20 15:27:53 -0800345 // bos pop case (php)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700346 ForwardingObjective.Builder fwdObjBosBuilder =
347 getMplsForwardingObjective(deviceId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700348 nextHops,
349 true,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700350 true,
Saurav Das8a0732e2015-11-20 15:27:53 -0800351 metabuilder.build());
352 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700353 return false;
354 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800355 fwdObjBuilders.add(fwdObjBosBuilder);
356
357 // XXX not-bos pop case, SR app multi-label not implemented yet
358 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
359 getMplsForwardingObjective(deviceId,
360 nextHops,
361 true,
362 false);*/
363
sanghob35a6192015-04-01 13:05:26 -0700364 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800365 // next hop is not destination, SR CONTINUE case (swap with self)
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700366 log.debug("Installing MPLS forwarding objective for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800367 + "label {} in switch {} without pop", segmentId, deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700368
Saurav Das8a0732e2015-11-20 15:27:53 -0800369 // continue case with bos - this does get triggered in edge routers
370 // and in core routers - driver can handle depending on availability
371 // of MPLS ECMP or not
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700372 ForwardingObjective.Builder fwdObjBosBuilder =
373 getMplsForwardingObjective(deviceId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700374 nextHops,
375 false,
Saurav Das8a0732e2015-11-20 15:27:53 -0800376 true,
377 metabuilder.build());
378 if (fwdObjBosBuilder == null) {
sanghob35a6192015-04-01 13:05:26 -0700379 return false;
380 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800381 fwdObjBuilders.add(fwdObjBosBuilder);
382
383 // XXX continue case with not-bos - SR app multi label not implemented yet
384 // also requires MPLS ECMP
385 /*ForwardingObjective.Builder fwdObjNoBosBuilder =
386 getMplsForwardingObjective(deviceId,
387 nextHops,
388 false,
389 false); */
390
sanghob35a6192015-04-01 13:05:26 -0700391 }
392
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700393 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
394 ((Builder) ((Builder) fwdObjBuilder.fromApp(srManager.appId)
395 .makePermanent()).withSelector(selector)
Charles Chane849c192016-01-11 18:28:54 -0800396 .withPriority(DEFAULT_PRIORITY))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700397 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700398 srManager.flowObjectiveService.
399 forward(deviceId,
400 fwdObjBuilder.
401 add(new SRObjectiveContext(deviceId,
Saurav Das8a0732e2015-11-20 15:27:53 -0800402 SRObjectiveContext.ObjectiveType.FORWARDING)));
sangho20eff1d2015-04-13 15:15:58 -0700403 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700404 }
405
406 return true;
407 }
408
Saurav Das8a0732e2015-11-20 15:27:53 -0800409 private ForwardingObjective.Builder getMplsForwardingObjective(
410 DeviceId deviceId,
411 Set<DeviceId> nextHops,
412 boolean phpRequired,
413 boolean isBos,
414 TrafficSelector meta) {
415
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700416 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
417 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700418
419 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
420
421 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800422 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700423 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700424 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700425 if (isBos) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800426 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType())
427 .decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700428 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800429 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
430 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700431 }
432 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800433 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700434 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700435 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700436 }
437
Saurav Das8a0732e2015-11-20 15:27:53 -0800438 // All forwarding is via ECMP group, the metadata informs the driver
439 // that the next-Objective will be used by MPLS flows. In other words,
440 // MPLS ECMP is requested. It is up to the driver to decide if these
441 // packets will be hashed or not.
442 fwdBuilder.withTreatment(tbuilder.build());
443 NeighborSet ns = new NeighborSet(nextHops);
444 log.debug("Trying to get a nextObjid for mpls rule on device:{} to ns:{}",
445 deviceId, ns);
446
447 int nextId = srManager.getNextObjectiveId(deviceId, ns, meta);
448 if (nextId <= 0) {
449 log.warn("No next objective in {} for ns: {}", deviceId, ns);
450 return null;
sanghob35a6192015-04-01 13:05:26 -0700451 }
452
Saurav Das8a0732e2015-11-20 15:27:53 -0800453 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700454 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700455 }
456
457 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700458 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700459 * dstMac corresponding to the router's MAC address. For those pipelines
460 * that need to internally assign vlans to untagged packets, this method
461 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700462 * <p>
463 * Note that the vlan assignment is only done by the master-instance for a switch.
464 * However we send the filtering objective from slave-instances as well, so
465 * that drivers can obtain other information (like Router MAC and IP).
sanghob35a6192015-04-01 13:05:26 -0700466 *
Saurav Das822c4e22015-10-23 10:51:11 -0700467 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700468 */
Saurav Das822c4e22015-10-23 10:51:11 -0700469 public void populateRouterMacVlanFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700470 log.debug("Installing per-port filtering objective for untagged "
471 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800472
473 MacAddress deviceMac;
474 try {
475 deviceMac = config.getDeviceMac(deviceId);
476 } catch (DeviceConfigNotFoundException e) {
477 log.warn(e.getMessage() + " Aborting populateRouterMacVlanFilters.");
478 return;
479 }
480
Saurav Das0e99e2b2015-10-28 12:39:42 -0700481 for (Port port : srManager.deviceService.getPorts(deviceId)) {
482 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
483 Ip4Prefix portSubnet = config.getPortSubnet(deviceId, port.number());
484 VlanId assignedVlan = (portSubnet == null)
485 ? VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET)
486 : srManager.getSubnetAssignedVlanId(deviceId, portSubnet);
Charles Chan0b4e6182015-11-03 10:42:14 -0800487
Saurav Das0e99e2b2015-10-28 12:39:42 -0700488 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
489 fob.withKey(Criteria.matchInPort(port.number()))
Charles Chan0b4e6182015-11-03 10:42:14 -0800490 .addCondition(Criteria.matchEthDst(deviceMac))
Charles Chane849c192016-01-11 18:28:54 -0800491 .addCondition(Criteria.matchVlanId(VlanId.NONE))
492 .withPriority(DEFAULT_PRIORITY);
Saurav Das837e0bb2015-10-30 17:45:38 -0700493 // vlan assignment is valid only if this instance is master
494 if (srManager.mastershipService.isLocalMaster(deviceId)) {
495 TrafficTreatment tt = DefaultTrafficTreatment.builder()
496 .pushVlan().setVlanId(assignedVlan).build();
Saurav Das4ce45962015-11-24 23:21:05 -0800497 fob.withMeta(tt);
Saurav Das837e0bb2015-10-30 17:45:38 -0700498 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700499 fob.permit().fromApp(srManager.appId);
500 srManager.flowObjectiveService.
501 filter(deviceId, fob.add(new SRObjectiveContext(deviceId,
502 SRObjectiveContext.ObjectiveType.FILTER)));
503 }
504 }
sanghob35a6192015-04-01 13:05:26 -0700505 }
506
507 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700508 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700509 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700510 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700511 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700512 *
Saurav Das822c4e22015-10-23 10:51:11 -0700513 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700514 */
Saurav Das822c4e22015-10-23 10:51:11 -0700515 public void populateRouterIpPunts(DeviceId deviceId) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800516 Ip4Address routerIp;
517 try {
518 routerIp = config.getRouterIp(deviceId);
519 } catch (DeviceConfigNotFoundException e) {
520 log.warn(e.getMessage() + " Aborting populateRouterIpPunts.");
521 return;
522 }
523
Saurav Das837e0bb2015-10-30 17:45:38 -0700524 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
525 log.debug("Not installing port-IP punts - not the master for dev:{} ",
526 deviceId);
527 return;
528 }
Saurav Das822c4e22015-10-23 10:51:11 -0700529 ForwardingObjective.Builder puntIp = DefaultForwardingObjective.builder();
Saurav Das837e0bb2015-10-30 17:45:38 -0700530 Set<Ip4Address> allIps = new HashSet<Ip4Address>(config.getPortIPs(deviceId));
Charles Chan0b4e6182015-11-03 10:42:14 -0800531 allIps.add(routerIp);
Saurav Das837e0bb2015-10-30 17:45:38 -0700532 for (Ip4Address ipaddr : allIps) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800533 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
534 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
535 sbuilder.matchEthType(Ethernet.TYPE_IPV4);
536 sbuilder.matchIPDst(IpPrefix.valueOf(ipaddr,
Saurav Das822c4e22015-10-23 10:51:11 -0700537 IpPrefix.MAX_INET_MASK_LENGTH));
Charles Chan68aa62d2015-11-09 16:37:23 -0800538 tbuilder.setOutput(PortNumber.CONTROLLER);
539 puntIp.withSelector(sbuilder.build());
540 puntIp.withTreatment(tbuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700541 puntIp.withFlag(Flag.VERSATILE)
542 .withPriority(HIGHEST_PRIORITY)
543 .makePermanent()
544 .fromApp(srManager.appId);
545 log.debug("Installing forwarding objective to punt port IP addresses");
546 srManager.flowObjectiveService.
547 forward(deviceId,
548 puntIp.add(new SRObjectiveContext(deviceId,
549 SRObjectiveContext.ObjectiveType.FORWARDING)));
550 }
sanghob35a6192015-04-01 13:05:26 -0700551 }
552
Charles Chan68aa62d2015-11-09 16:37:23 -0800553 /**
554 * Populates a forwarding objective to send packets that miss other high
555 * priority Bridging Table entries to a group that contains all ports of
556 * its subnet.
557 *
558 * Note: We assume that packets sending from the edge switches to the hosts
559 * have untagged VLAN.
560 * The VLAN tag will be popped later in the flooding group.
561 *
562 * @param deviceId switch ID to set the rules
563 */
564 public void populateSubnetBroadcastRule(DeviceId deviceId) {
565 config.getSubnets(deviceId).forEach(subnet -> {
566 int nextId = srManager.getSubnetNextObjectiveId(deviceId, subnet);
567 VlanId vlanId = srManager.getSubnetAssignedVlanId(deviceId, subnet);
568
Saurav Das4ce45962015-11-24 23:21:05 -0800569 if (nextId < 0 || vlanId == null) {
570 log.error("Cannot install subnet broadcast rule in dev:{} due"
571 + "to vlanId:{} or nextId:{}", vlanId, nextId);
572 return;
573 }
574
Charles Chan68aa62d2015-11-09 16:37:23 -0800575 /* Driver should treat objective with MacAddress.NONE as the
576 * subnet broadcast rule
577 */
578 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
579 sbuilder.matchVlanId(vlanId);
580 sbuilder.matchEthDst(MacAddress.NONE);
581
582 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
583 fob.withFlag(Flag.SPECIFIC)
584 .withSelector(sbuilder.build())
585 .nextStep(nextId)
Charles Chane849c192016-01-11 18:28:54 -0800586 .withPriority(FLOOD_PRIORITY)
Charles Chan68aa62d2015-11-09 16:37:23 -0800587 .fromApp(srManager.appId)
588 .makePermanent();
589
590 srManager.flowObjectiveService.forward(
591 deviceId,
592 fob.add(new SRObjectiveContext(
593 deviceId,
594 SRObjectiveContext.ObjectiveType.FORWARDING)
595 )
596 );
597 });
598 }
599
Charles Chane849c192016-01-11 18:28:54 -0800600 /**
601 * Creates a filtering objective to permit VLAN cross-connect traffic.
602 *
603 * @param deviceId the DPID of the switch
604 */
605 public void populateXConnectVlanFilters(DeviceId deviceId) {
606 Map<VlanId, List<ConnectPoint>> xConnectsForDevice =
607 config.getXConnects();
608 xConnectsForDevice.forEach((vlanId, connectPoints) -> {
609 // Only proceed the xConnect for given device
610 for (ConnectPoint connectPoint : connectPoints) {
611 if (!connectPoint.deviceId().equals(deviceId)) {
612 return;
613 }
614 }
615
616 connectPoints.forEach(connectPoint -> {
617 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
618 fob.withKey(Criteria.matchInPort(connectPoint.port()))
619 .addCondition(Criteria.matchVlanId(vlanId))
620 .addCondition(Criteria.matchEthDst(MacAddress.NONE))
621 .withPriority(XCONNECT_PRIORITY);
622
623 fob.permit().fromApp(srManager.appId);
624 srManager.flowObjectiveService
625 .filter(deviceId, fob.add(new SRObjectiveContext(deviceId,
626 SRObjectiveContext.ObjectiveType.FILTER)));
627 });
628 });
629 }
630
631 /**
632 * Populates a forwarding objective that points the VLAN cross-connect
633 * packets to a broadcast group.
634 *
635 * @param deviceId switch ID to set the rules
636 */
637 public void populateXConnectBroadcastRule(DeviceId deviceId) {
638 Map<VlanId, List<ConnectPoint>> xConnects =
639 config.getXConnects();
640 xConnects.forEach((vlanId, connectPoints) -> {
641 // Only proceed the xConnect for given device
642 for (ConnectPoint connectPoint : connectPoints) {
643 if (!connectPoint.deviceId().equals(deviceId)) {
644 return;
645 }
646 }
647
648 int nextId = srManager.getXConnectNextObjectiveId(deviceId, vlanId);
649 if (nextId < 0) {
650 log.error("Cannot install cross-connect broadcast rule in dev:{} " +
651 "due to missing nextId:{}", deviceId, nextId);
652 return;
653 }
654
655 /*
656 * Driver should treat objectives with MacAddress.NONE and !VlanId.NONE
657 * as the VLAN cross-connect broadcast rules
658 */
659 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
660 sbuilder.matchVlanId(vlanId);
661 sbuilder.matchEthDst(MacAddress.NONE);
662
663 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
664 fob.withFlag(Flag.SPECIFIC)
665 .withSelector(sbuilder.build())
666 .nextStep(nextId)
667 .withPriority(DEFAULT_PRIORITY)
668 .fromApp(srManager.appId)
669 .makePermanent();
670
671 srManager.flowObjectiveService.forward(
672 deviceId,
673 fob.add(new SRObjectiveContext(
674 deviceId,
675 SRObjectiveContext.ObjectiveType.FORWARDING)
676 )
677 );
678 });
679 }
Charles Chan68aa62d2015-11-09 16:37:23 -0800680
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700681 private static class SRObjectiveContext implements ObjectiveContext {
682 enum ObjectiveType {
683 FILTER,
684 FORWARDING
685 }
686 final DeviceId deviceId;
687 final ObjectiveType type;
688
689 SRObjectiveContext(DeviceId deviceId, ObjectiveType type) {
690 this.deviceId = deviceId;
691 this.type = type;
692 }
693 @Override
694 public void onSuccess(Objective objective) {
695 log.debug("{} objective operation successful in device {}",
696 type.name(), deviceId);
697 }
698
699 @Override
700 public void onError(Objective objective, ObjectiveError error) {
701 log.warn("{} objective {} operation failed with error: {} in device {}",
702 type.name(), objective, error, deviceId);
703 }
704 }
705
sanghob35a6192015-04-01 13:05:26 -0700706}