blob: 7ba893a0c21a58f9919df4099720ddd8f05777f0 [file] [log] [blame]
Cem Türker3baff672017-10-12 15:09:01 +03001/*
2 * Copyright 2017-present Open Networking Foundation
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.provider.of.flow.impl;
17
18import com.google.common.collect.Lists;
19import com.google.common.collect.Sets;
20import org.onosproject.net.PortNumber;
21import org.onosproject.net.driver.DriverService;
22import org.onosproject.net.flow.FlowRule;
23import org.onosproject.net.flow.StatTriggerField;
24import org.onosproject.net.flow.StatTriggerFlag;
25import org.onosproject.net.flow.instructions.Instruction;
26import org.onosproject.net.flow.instructions.Instructions;
27import org.projectfloodlight.openflow.protocol.OFFactory;
28import org.projectfloodlight.openflow.protocol.OFFlowAdd;
29import org.projectfloodlight.openflow.protocol.OFFlowMod;
30import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
31import org.projectfloodlight.openflow.protocol.OFOxsList;
32import org.projectfloodlight.openflow.protocol.OFStatTriggerFlags;
33import org.projectfloodlight.openflow.protocol.action.OFAction;
34import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
35import org.projectfloodlight.openflow.protocol.action.OFActionMeter;
36import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
37import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue;
38import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
39import org.projectfloodlight.openflow.protocol.match.Match;
40import org.projectfloodlight.openflow.protocol.oxs.OFOxs;
41import org.projectfloodlight.openflow.types.OFBufferId;
42import org.projectfloodlight.openflow.types.OFGroup;
43import org.projectfloodlight.openflow.types.OFPort;
44import org.projectfloodlight.openflow.types.TableId;
45import org.projectfloodlight.openflow.types.U32;
46import org.projectfloodlight.openflow.types.U64;
47
48import java.util.Collections;
49import java.util.LinkedList;
50import java.util.List;
51import java.util.Map;
52import java.util.Optional;
53import java.util.Set;
54
55import static org.projectfloodlight.openflow.protocol.OFStatTriggerFlags.ONLY_FIRST;
56import static org.projectfloodlight.openflow.protocol.OFStatTriggerFlags.PERIODIC;
57
58/**
59 * Flow mod builder for OpenFlow 1.5+.
60 */
61public class FlowModBuilderVer15 extends FlowModBuilderVer13 {
62
63 /**
64 * Constructor for a flow mod builder for OpenFlow 1.5.
65 *
66 * @param flowRule the flow rule to transform into a flow mod
67 * @param factory the OpenFlow factory to use to build the flow mod
68 * @param xid the transaction ID
69 * @param driverService the device driver service
70 */
71 protected FlowModBuilderVer15(FlowRule flowRule, OFFactory factory,
72 Optional<Long> xid,
73 Optional<DriverService> driverService) {
74 super(flowRule, factory, xid, driverService);
75 }
76
77 @Override
78 public OFFlowMod buildFlowAdd() {
79 Match match = buildMatch();
80 List<OFAction> deferredActions = buildActions(treatment.deferred(), false);
81 List<OFAction> immediateActions = buildActions(treatment.immediate(), true);
82 List<OFInstruction> instructions = Lists.newLinkedList();
83
84 if (treatment.clearedDeferred()) {
85 instructions.add(factory().instructions().clearActions());
86 }
87 if (!immediateActions.isEmpty()) {
88 instructions.add(factory().instructions().applyActions(immediateActions));
89 }
90 if (!deferredActions.isEmpty()) {
91 instructions.add(factory().instructions().writeActions(deferredActions));
92 }
93 if (treatment.tableTransition() != null) {
94 instructions.add(buildTableGoto(treatment.tableTransition()));
95 }
96 if (treatment.writeMetadata() != null) {
97 instructions.add(buildMetadata(treatment.writeMetadata()));
98 }
99 if (treatment.statTrigger() != null) {
100 instructions.add(buildStatTrigger(treatment.statTrigger()));
101 }
102
103
104
105 long cookie = flowRule().id().value();
106
107 OFFlowAdd fm = factory().buildFlowAdd()
108 .setXid(xid)
109 .setCookie(U64.of(cookie))
110 .setBufferId(OFBufferId.NO_BUFFER)
111 .setInstructions(instructions)
112 .setMatch(match)
113 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
114 .setPriority(flowRule().priority())
115 .setTableId(TableId.of(flowRule().tableId()))
116 .setHardTimeout(flowRule().hardTimeout())
117 .build();
118
119 return fm;
120 }
121
122 @Override
123 public OFFlowMod buildFlowMod() {
124 Match match = buildMatch();
125 List<OFAction> deferredActions = buildActions(treatment.deferred(), false);
126 List<OFAction> immediateActions = buildActions(treatment.immediate(), true);
127 List<OFInstruction> instructions = Lists.newLinkedList();
128
129
130 if (!immediateActions.isEmpty()) {
131 instructions.add(factory().instructions().applyActions(immediateActions));
132 }
133 if (treatment.clearedDeferred()) {
134 instructions.add(factory().instructions().clearActions());
135 }
136 if (!deferredActions.isEmpty()) {
137 instructions.add(factory().instructions().writeActions(deferredActions));
138 }
139 if (treatment.tableTransition() != null) {
140 instructions.add(buildTableGoto(treatment.tableTransition()));
141 }
142 if (treatment.writeMetadata() != null) {
143 instructions.add(buildMetadata(treatment.writeMetadata()));
144 }
145 if (treatment.statTrigger() != null) {
146 instructions.add(buildStatTrigger(treatment.statTrigger()));
147 }
148
149 long cookie = flowRule().id().value();
150
151 OFFlowMod fm = factory().buildFlowModify()
152 .setXid(xid)
153 .setCookie(U64.of(cookie))
154 .setBufferId(OFBufferId.NO_BUFFER)
155 .setInstructions(instructions)
156 .setMatch(match)
157 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
158 .setPriority(flowRule().priority())
159 .setTableId(TableId.of(flowRule().tableId()))
160 .setHardTimeout(flowRule().hardTimeout())
161 .build();
162
163 return fm;
164 }
165
166 private List<OFAction> buildActions(List<Instruction> treatments, Boolean immediateActions) {
167 if (treatment == null) {
168 return Collections.emptyList();
169 }
170
171 boolean tableFound = false;
172 List<OFAction> actions = new LinkedList<>();
173
174 //Meter action handling
175 if (null != treatment.metered() && immediateActions) {
176 OFAction meterAction = buildMultipleMeterAction(treatment.metered());
177 actions.add(meterAction);
178 }
179
180 for (Instruction i : treatments) {
181 switch (i.type()) {
182 case NOACTION:
183 return Collections.emptyList();
184 case L0MODIFICATION:
185 actions.add(buildL0Modification(i));
186 break;
187 case L1MODIFICATION:
188 actions.add(buildL1Modification(i));
189 break;
190 case L2MODIFICATION:
191 actions.add(buildL2Modification(i));
192 break;
193 case L3MODIFICATION:
194 actions.add(buildL3Modification(i));
195 break;
196 case L4MODIFICATION:
197 actions.add(buildL4Modification(i));
198 break;
199 case OUTPUT:
200 Instructions.OutputInstruction out = (Instructions.OutputInstruction) i;
201 OFActionOutput.Builder action = factory().actions().buildOutput()
202 .setPort(OFPort.of((int) out.port().toLong()));
203 if (out.port().equals(PortNumber.CONTROLLER)) {
204 action.setMaxLen(OFPCML_NO_BUFFER);
205 }
206 actions.add(action.build());
207 break;
208 case GROUP:
209 Instructions.GroupInstruction group = (Instructions.GroupInstruction) i;
210 OFActionGroup.Builder groupBuilder = factory().actions().buildGroup()
211 .setGroup(OFGroup.of(group.groupId().id()));
212 actions.add(groupBuilder.build());
213 break;
214 case QUEUE:
215 Instructions.SetQueueInstruction queue = (Instructions.SetQueueInstruction) i;
216 OFActionSetQueue.Builder queueBuilder = factory().actions().buildSetQueue()
217 .setQueueId(queue.queueId());
218 actions.add(queueBuilder.build());
219 break;
220 case TABLE:
221 //FIXME: should not occur here.
222 tableFound = true;
223 break;
224 case EXTENSION:
225 actions.add(buildExtensionAction(((Instructions.ExtensionInstructionWrapper) i)
226 .extensionInstruction()));
227 break;
228 default:
229 log.warn("Instruction type {} not yet implemented.", i.type());
230 }
231 }
232
233 if (tableFound && actions.isEmpty()) {
234 // handles the case where there are no actions, but there is
235 // a goto instruction for the next table
236 return Collections.emptyList();
237 }
238 return actions;
239 }
240
241 private OFOxsList getOFOxsList(Map<StatTriggerField, Long> statTriggerMap) {
242 OFFactory factory = factory();
243 List<OFOxs<?>> ofOxsList = Lists.newArrayList();
244 for (Map.Entry<StatTriggerField, Long> entry : statTriggerMap.entrySet()) {
245 switch (entry.getKey()) {
246 case DURATION:
247 ofOxsList.add(factory.oxss().buildDuration().setValue(U64.of(entry.getValue())).build());
248 break;
249 case IDLE_TIME:
250 ofOxsList.add(factory.oxss().buildIdleTime().setValue(U64.of(entry.getValue())).build());
251 break;
252 case BYTE_COUNT:
253 ofOxsList.add(factory.oxss().buildByteCount().setValue(U64.of(entry.getValue())).build());
254 break;
255 case FLOW_COUNT:
256 ofOxsList.add(factory.oxss().buildFlowCount().setValue(U32.of(entry.getValue())).build());
257 break;
258 case PACKET_COUNT:
259 ofOxsList.add(factory.oxss().buildPacketCount().setValue(U64.of(entry.getValue())).build());
260 break;
261 default:
262 log.warn("Unsupported Stat Trigger field");
263 break;
264 }
265 }
266 return OFOxsList.ofList(ofOxsList);
267 }
268
269 private Set<OFStatTriggerFlags> getStatTriggerFlag(StatTriggerFlag flag) {
270 Set<OFStatTriggerFlags> statTriggerFlagsSet = Sets.newHashSet();
271 switch (flag) {
272 case PERIODIC:
273 statTriggerFlagsSet.add(PERIODIC);
274 break;
275 case ONLY_FIRST:
276 statTriggerFlagsSet.add(ONLY_FIRST);
277 break;
278 default:
279 break;
280 }
281 return statTriggerFlagsSet;
282 }
283
284 /**
285 * Meter action builder.
286 *
287 * @param meterInstruction meter instruction
288 * @return meter action
289 */
290 protected OFAction buildMultipleMeterAction(Instructions.MeterInstruction meterInstruction) {
291 OFActionMeter.Builder meterBuilder = factory().actions().buildMeter()
292 .setMeterId(meterInstruction.meterId().id());
293 return meterBuilder.build();
294 }
295
296 protected OFInstruction buildStatTrigger(Instructions.StatTriggerInstruction s) {
297 OFInstruction instruction = factory().instructions().statTrigger(getStatTriggerFlag(s.getStatTriggerFlag()),
298 getOFOxsList(s.getStatTriggerFieldMap()));
299 return instruction;
300 }
301}