blob: e0e50995e914176b44b404812087086b1632c1b7 [file] [log] [blame]
Charles Chanf9e98652016-09-07 16:54:23 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Charles Chanf9e98652016-09-07 16:54:23 -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 */
16
Yi Tsengef19de12017-04-24 11:33:05 -070017package org.onosproject.driver.pipeline.ofdpa;
Charles Chanf9e98652016-09-07 16:54:23 -070018
Pier Ventre42287df2016-11-09 14:17:26 -080019import com.google.common.collect.Lists;
Charles Chanf9e98652016-09-07 16:54:23 -070020import org.onosproject.core.ApplicationId;
Yi Tsengfa394de2017-02-01 11:26:40 -080021import org.onosproject.core.GroupId;
Pier Ventre42287df2016-11-09 14:17:26 -080022import org.onosproject.driver.extensions.Ofdpa3PushCw;
23import org.onosproject.driver.extensions.Ofdpa3PushL2Header;
24import org.onosproject.net.flow.DefaultTrafficTreatment;
Charles Chanf9e98652016-09-07 16:54:23 -070025import org.onosproject.net.flow.TrafficSelector;
26import org.onosproject.net.flow.TrafficTreatment;
Pier Ventre42287df2016-11-09 14:17:26 -080027import org.onosproject.net.flow.instructions.Instruction;
28import org.onosproject.net.flow.instructions.L2ModificationInstruction;
29import org.onosproject.net.flow.instructions.L3ModificationInstruction;
30import org.onosproject.net.flowobjective.NextObjective;
31import org.onosproject.net.flowobjective.ObjectiveError;
32import org.onosproject.net.group.DefaultGroupBucket;
33import org.onosproject.net.group.DefaultGroupDescription;
34import org.onosproject.net.group.DefaultGroupKey;
35import org.onosproject.net.group.GroupBucket;
36import org.onosproject.net.group.GroupBuckets;
37import org.onosproject.net.group.GroupDescription;
38import org.onosproject.net.group.GroupKey;
39import org.slf4j.Logger;
40
41import java.util.ArrayDeque;
42import java.util.Collections;
43import java.util.Deque;
44import java.util.List;
45
Yi Tsengef19de12017-04-24 11:33:05 -070046import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Pier Ventre42287df2016-11-09 14:17:26 -080047import static org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType.TTL_OUT;
48import static org.onosproject.net.group.GroupDescription.Type.INDIRECT;
49import static org.slf4j.LoggerFactory.getLogger;
Charles Chanf9e98652016-09-07 16:54:23 -070050
51/**
52 * Group handler for OFDPA2 pipeline.
53 */
54public class Ofdpa3GroupHandler extends Ofdpa2GroupHandler {
Pier Ventre42287df2016-11-09 14:17:26 -080055
56 private static final int PW_INTERNAL_VLAN = 4094;
57 private static final int MAX_DEPTH_UNPROTECTED_PW = 3;
58
59 private final Logger log = getLogger(getClass());
60
Charles Chanf9e98652016-09-07 16:54:23 -070061 @Override
62 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
63 ApplicationId appId, boolean mpls,
64 TrafficSelector meta) {
65 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, false);
66 }
Pier Ventre42287df2016-11-09 14:17:26 -080067
68 @Override
69 protected void processPwNextObjective(NextObjective nextObjective) {
Andreas Pantelopoulos5253c712017-12-20 18:04:27 -080070
71 log.info("Started deploying nextObjective id={} for pseudowire", nextObjective.id());
72
Pier Ventre42287df2016-11-09 14:17:26 -080073 TrafficTreatment treatment = nextObjective.next().iterator().next();
74 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
75 GroupChainElem groupChainElem;
76 GroupKey groupKey;
77 GroupDescription groupDescription;
78 // Now we separate the mpls actions from the l2/l3 actions
79 TrafficTreatment.Builder l2L3Treatment = DefaultTrafficTreatment.builder();
80 TrafficTreatment.Builder mplsTreatment = DefaultTrafficTreatment.builder();
81 createL2L3AndMplsTreatments(treatment, l2L3Treatment, mplsTreatment);
82 // We create the chain from mpls intf group to
83 // l2 intf group.
84 GroupInfo groupInfo = createL2L3ChainInternal(
85 l2L3Treatment.build(),
86 nextObjective.id(),
87 nextObjective.appId(),
88 true,
89 nextObjective.meta(),
90 false
91 );
92 if (groupInfo == null) {
93 log.error("Could not process nextObj={} in dev:{}", nextObjective.id(), deviceId);
94 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.GROUPINSTALLATIONFAILED);
95 return;
96 }
97 // We update the chain with the last two groups;
Yi Tsengef19de12017-04-24 11:33:05 -070098 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
99 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Pier Ventre42287df2016-11-09 14:17:26 -0800100 // We retrieve also all mpls instructions.
101 List<List<Instruction>> mplsInstructionSets = Lists.newArrayList();
102 List<Instruction> mplsInstructionSet = Lists.newArrayList();
103 L3ModificationInstruction l3Ins;
104 for (Instruction ins : treatment.allInstructions()) {
105 // Each mpls instruction set is delimited by a
106 // copy ttl outward action.
107 mplsInstructionSet.add(ins);
108 if (ins.type() == Instruction.Type.L3MODIFICATION) {
109 l3Ins = (L3ModificationInstruction) ins;
110 if (l3Ins.subtype() == TTL_OUT) {
111 mplsInstructionSets.add(mplsInstructionSet);
112 mplsInstructionSet = Lists.newArrayList();
113 }
Pier Ventre42287df2016-11-09 14:17:26 -0800114 }
115 }
Andreas Pantelopoulos5253c712017-12-20 18:04:27 -0800116
Pier Ventre42287df2016-11-09 14:17:26 -0800117 if (mplsInstructionSets.size() > MAX_DEPTH_UNPROTECTED_PW) {
118 log.error("Next Objective for pseudo wire should have at "
119 + "most {} mpls instruction sets. Next Objective Id:{}",
120 MAX_DEPTH_UNPROTECTED_PW, nextObjective.id());
121 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
122 return;
123 }
Andreas Pantelopoulos5253c712017-12-20 18:04:27 -0800124
125 log.debug("Size of mpls instructions is {}.", mplsInstructionSets.size());
126 log.debug("mpls instructions sets are {}.", mplsInstructionSets);
127
Yi Tsengef19de12017-04-24 11:33:05 -0700128 int nextGid = groupInfo.nextGroupDesc().givenGroupId();
Pier Ventre42287df2016-11-09 14:17:26 -0800129 int index;
Andreas Pantelopoulos5253c712017-12-20 18:04:27 -0800130
Pier Ventre42287df2016-11-09 14:17:26 -0800131 // We create the mpls tunnel label groups.
132 // In this case we need to use also the
133 // tunnel label group 2;
Andreas Pantelopoulos5253c712017-12-20 18:04:27 -0800134 // this is for inter-co pws
Pier Ventre42287df2016-11-09 14:17:26 -0800135 if (mplsInstructionSets.size() == MAX_DEPTH_UNPROTECTED_PW) {
Andreas Pantelopoulos5253c712017-12-20 18:04:27 -0800136
137 log.debug("Creating inter-co pw mpls chains with nextid {}", nextObjective.id());
138
Pier Ventre42287df2016-11-09 14:17:26 -0800139 // We deal with the label 2 group.
140 index = getNextAvailableIndex();
141 groupDescription = createMplsTunnelLabelGroup(
142 nextGid,
Yi Tsengef19de12017-04-24 11:33:05 -0700143 OfdpaMplsGroupSubType.MPLS_TUNNEL_LABEL_2,
Pier Ventre42287df2016-11-09 14:17:26 -0800144 index,
145 mplsInstructionSets.get(2),
146 nextObjective.appId()
147 );
148 groupKey = new DefaultGroupKey(
149 Ofdpa2Pipeline.appKryo.serialize(index)
150 );
151 // We update the chain.
Yi Tsengef19de12017-04-24 11:33:05 -0700152 groupChainElem = new GroupChainElem(groupDescription, 1, false, deviceId);
Pier Ventre42287df2016-11-09 14:17:26 -0800153 updatePendingGroups(
Yi Tsengef19de12017-04-24 11:33:05 -0700154 groupInfo.nextGroupDesc().appCookie(),
Pier Ventre42287df2016-11-09 14:17:26 -0800155 groupChainElem
156 );
157 gkeyChain.addFirst(groupKey);
158 // We have to create tunnel label group and
159 // l2 vpn group before to send the inner most
160 // group. We update the nextGid.
161 nextGid = groupDescription.givenGroupId();
Yi Tsengef19de12017-04-24 11:33:05 -0700162 groupInfo = new GroupInfo(groupInfo.innerMostGroupDesc(), groupDescription);
Pier Ventre42287df2016-11-09 14:17:26 -0800163
164 log.debug("Trying Label 2 Group: device:{} gid:{} gkey:{} nextId:{}",
165 deviceId, Integer.toHexString(nextGid),
166 groupKey, nextObjective.id());
167 }
Pier Ventre42287df2016-11-09 14:17:26 -0800168
Andreas Pantelopoulos5253c712017-12-20 18:04:27 -0800169 // if treatment has 2 mpls labels, then this is a pseudowire from leaf to another leaf
170 // inside a single co
171 if (mplsInstructionSets.size() == 2) {
Pier Ventre42287df2016-11-09 14:17:26 -0800172
Andreas Pantelopoulos5253c712017-12-20 18:04:27 -0800173 log.debug("Creating leaf-leaf pw mpls chains with nextid {}", nextObjective.id());
174 // We deal with the label 1 group.
175 index = getNextAvailableIndex();
176 groupDescription = createMplsTunnelLabelGroup(nextGid,
177 OfdpaMplsGroupSubType.MPLS_TUNNEL_LABEL_1,
178 index,
179 mplsInstructionSets.get(1),
180 nextObjective.appId());
181 groupKey = new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(index));
182 groupChainElem = new GroupChainElem(groupDescription, 1, false, deviceId);
183 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), groupChainElem);
184 gkeyChain.addFirst(groupKey);
185 // We have to create the l2 vpn group before
186 // to send the inner most group.
187 nextGid = groupDescription.givenGroupId();
188 groupInfo = new GroupInfo(groupInfo.innerMostGroupDesc(), groupDescription);
189
190 log.debug("Trying Label 1 Group: device:{} gid:{} gkey:{} nextId:{}",
191 deviceId, Integer.toHexString(nextGid),
192 groupKey, nextObjective.id());
193 // Finally we create the l2 vpn group.
194 index = getNextAvailableIndex();
195 groupDescription = createMplsL2VpnGroup(nextGid, index,
196 mplsInstructionSets.get(0), nextObjective.appId());
197 groupKey = new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(index));
198 groupChainElem = new GroupChainElem(groupDescription, 1, false, deviceId);
199 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), groupChainElem);
200 gkeyChain.addFirst(groupKey);
201 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObjective);
202 updatePendingNextObjective(groupKey, ofdpaGrp);
203
204 log.debug("Trying L2 Vpn Group: device:{} gid:{} gkey:{} nextId:{}", deviceId,
205 Integer.toHexString(nextGid), groupKey, nextObjective.id());
206 // Finally we send the innermost group.
207 log.debug("Sending innermost group {} in group chain on device {} ",
208 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()), deviceId);
209 groupService.addGroup(groupInfo.innerMostGroupDesc());
210 }
211
212 // this is a pseudowire from leaf to spine,
213 // only one label is used
214 if (mplsInstructionSets.size() == 1) {
215
216 log.debug("Creating leaf-spine pw mpls chains with nextid {}", nextObjective.id());
217
218 // Finally we create the l2 vpn group.
219 index = getNextAvailableIndex();
220 groupDescription = createMplsL2VpnGroup(nextGid, index, mplsInstructionSets.get(0),
221 nextObjective.appId());
222 groupKey = new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(index));
223 groupChainElem = new GroupChainElem(groupDescription, 1, false, deviceId);
224 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), groupChainElem);
225 gkeyChain.addFirst(groupKey);
226 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObjective);
227 updatePendingNextObjective(groupKey, ofdpaGrp);
228
229 log.debug("Trying L2 Vpn Group: device:{} gid:{} gkey:{} nextId:{}",
230 deviceId, Integer.toHexString(nextGid), groupKey, nextObjective.id());
231 // Finally we send the innermost group.
232 log.debug("Sending innermost group {} in group chain on device {} ",
233 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()), deviceId);
234 groupService.addGroup(groupInfo.innerMostGroupDesc());
235 }
Pier Ventre42287df2016-11-09 14:17:26 -0800236 }
237
238 /**
239 * Helper method to create a mpls tunnel label group.
240 *
241 * @param nextGroupId the next group in the chain
242 * @param subtype the mpls tunnel label group subtype
243 * @param index the index of the group
244 * @param instructions the instructions to push
245 * @param applicationId the application id
246 * @return the group description
247 */
248 private GroupDescription createMplsTunnelLabelGroup(int nextGroupId,
Yi Tsengef19de12017-04-24 11:33:05 -0700249 OfdpaMplsGroupSubType subtype,
250 int index,
251 List<Instruction> instructions,
252 ApplicationId applicationId) {
Pier Ventre42287df2016-11-09 14:17:26 -0800253 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
254 // We add all the instructions.
255 instructions.forEach(treatment::add);
256 // We point the group to the next group.
Yi Tsengfa394de2017-02-01 11:26:40 -0800257 treatment.group(new GroupId(nextGroupId));
Pier Ventre42287df2016-11-09 14:17:26 -0800258 GroupBucket groupBucket = DefaultGroupBucket
259 .createIndirectGroupBucket(treatment.build());
260 // Finally we build the group description.
261 int groupId = makeMplsLabelGroupId(subtype, index);
262 GroupKey groupKey = new DefaultGroupKey(
263 Ofdpa2Pipeline.appKryo.serialize(index)
264 );
265 return new DefaultGroupDescription(
266 deviceId,
267 INDIRECT,
268 new GroupBuckets(Collections.singletonList(groupBucket)),
269 groupKey,
270 groupId,
271 applicationId
272 );
273 }
274
275 /**
276 * Helper method to create a mpls l2 vpn group.
277 *
278 * @param nextGroupId the next group in the chain
279 * @param index the index of the group
280 * @param instructions the instructions to push
281 * @param applicationId the application id
282 * @return the group description
283 */
284 private GroupDescription createMplsL2VpnGroup(int nextGroupId,
Yi Tsengef19de12017-04-24 11:33:05 -0700285 int index,
286 List<Instruction> instructions,
287 ApplicationId applicationId) {
Pier Ventre42287df2016-11-09 14:17:26 -0800288 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
289 // We add the extensions and the instructions.
290 treatment.extension(new Ofdpa3PushL2Header(), deviceId);
291 treatment.pushVlan();
292 instructions.forEach(treatment::add);
293 treatment.extension(new Ofdpa3PushCw(), deviceId);
294 // We point the group to the next group.
Yi Tsengfa394de2017-02-01 11:26:40 -0800295 treatment.group(new GroupId(nextGroupId));
Pier Ventre42287df2016-11-09 14:17:26 -0800296 GroupBucket groupBucket = DefaultGroupBucket
297 .createIndirectGroupBucket(treatment.build());
298 // Finally we build the group description.
Yi Tsengef19de12017-04-24 11:33:05 -0700299 int groupId = makeMplsLabelGroupId(OfdpaMplsGroupSubType.L2_VPN, index);
Pier Ventre42287df2016-11-09 14:17:26 -0800300 GroupKey groupKey = new DefaultGroupKey(
301 Ofdpa2Pipeline.appKryo.serialize(index)
302 );
303 return new DefaultGroupDescription(
304 deviceId,
305 INDIRECT,
306 new GroupBuckets(Collections.singletonList(groupBucket)),
307 groupKey,
308 groupId,
309 applicationId
310 );
311 }
312
313 /**
314 * Helper method for dividing the l2/l3 instructions from the mpls
315 * instructions.
316 *
317 * @param treatment the treatment to analyze
318 * @param l2L3Treatment the l2/l3 treatment builder
319 * @param mplsTreatment the mpls treatment builder
320 */
321 private void createL2L3AndMplsTreatments(TrafficTreatment treatment,
Yi Tsengef19de12017-04-24 11:33:05 -0700322 TrafficTreatment.Builder l2L3Treatment,
323 TrafficTreatment.Builder mplsTreatment) {
Pier Ventre42287df2016-11-09 14:17:26 -0800324
325 for (Instruction ins : treatment.allInstructions()) {
326
327 if (ins.type() == Instruction.Type.L2MODIFICATION) {
328 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
329 switch (l2ins.subtype()) {
330 // These instructions have to go in the l2/l3 treatment.
331 case ETH_DST:
332 case ETH_SRC:
333 case VLAN_ID:
334 case VLAN_POP:
335 l2L3Treatment.add(ins);
336 break;
337 // These instructions have to go in the mpls treatment.
338 case MPLS_BOS:
339 case DEC_MPLS_TTL:
340 case MPLS_LABEL:
341 case MPLS_PUSH:
342 mplsTreatment.add(ins);
343 break;
344 default:
Saurav Dasa89b95a2018-02-14 14:14:54 -0800345 log.warn("Driver does not handle TrafficTreatment"
346 + " L2Mod {} for pw next-obj", l2ins.subtype());
Pier Ventre42287df2016-11-09 14:17:26 -0800347 break;
348 }
349 } else if (ins.type() == Instruction.Type.OUTPUT) {
350 // The output goes in the l2/l3 treatment.
351 l2L3Treatment.add(ins);
352 } else if (ins.type() == Instruction.Type.L3MODIFICATION) {
353 // We support partially the l3 instructions.
354 L3ModificationInstruction l3ins = (L3ModificationInstruction) ins;
355 switch (l3ins.subtype()) {
356 case TTL_OUT:
357 mplsTreatment.add(ins);
358 break;
359 default:
Saurav Dasa89b95a2018-02-14 14:14:54 -0800360 log.warn("Driver does not handle TrafficTreatment"
361 + " L3Mod for pw next-obj", l3ins.subtype());
Pier Ventre42287df2016-11-09 14:17:26 -0800362 }
363
364 } else {
365 log.warn("Driver does not handle this type of TrafficTreatment"
Saurav Dasa89b95a2018-02-14 14:14:54 -0800366 + " instruction for pw next-obj: {} - {}",
Pier Ventre42287df2016-11-09 14:17:26 -0800367 ins.type(), ins);
368 }
369 }
Pier Ventre42287df2016-11-09 14:17:26 -0800370 }
371 // TODO Introduce in the future an inner class to return two treatments
Charles Chanf9e98652016-09-07 16:54:23 -0700372}