2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.provider.of.flow.impl;
18 import com.google.common.collect.Lists;
19 import org.onlab.packet.Ip4Address;
20 import org.onlab.packet.Ip6Address;
21 import org.onosproject.net.OchSignal;
22 import org.onosproject.net.PortNumber;
23 import org.onosproject.net.flow.FlowRule;
24 import org.onosproject.net.flow.TrafficTreatment;
25 import org.onosproject.net.flow.instructions.Instruction;
26 import org.onosproject.net.flow.instructions.Instructions;
27 import org.onosproject.net.flow.instructions.Instructions.GroupInstruction;
28 import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
29 import org.onosproject.net.flow.instructions.Instructions.SetQueueInstruction;
30 import org.onosproject.net.flow.instructions.L0ModificationInstruction;
31 import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
32 import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction;
33 import org.onosproject.net.flow.instructions.L2ModificationInstruction;
34 import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
35 import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsBosInstruction;
36 import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction;
37 import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
38 import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
39 import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions;
40 import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModTunnelIdInstruction;
41 import org.onosproject.net.flow.instructions.L3ModificationInstruction;
42 import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
43 import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
44 import org.onosproject.net.flow.instructions.L4ModificationInstruction;
45 import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction;
46 import org.projectfloodlight.openflow.protocol.OFFactory;
47 import org.projectfloodlight.openflow.protocol.OFFlowAdd;
48 import org.projectfloodlight.openflow.protocol.OFFlowDelete;
49 import org.projectfloodlight.openflow.protocol.OFFlowMod;
50 import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
51 import org.projectfloodlight.openflow.protocol.action.OFAction;
52 import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
53 import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
54 import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue;
55 import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
56 import org.projectfloodlight.openflow.protocol.match.Match;
57 import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
58 import org.projectfloodlight.openflow.types.CircuitSignalID;
59 import org.projectfloodlight.openflow.types.EthType;
60 import org.projectfloodlight.openflow.types.IPv4Address;
61 import org.projectfloodlight.openflow.types.IPv6Address;
62 import org.projectfloodlight.openflow.types.IPv6FlowLabel;
63 import org.projectfloodlight.openflow.types.MacAddress;
64 import org.projectfloodlight.openflow.types.OFBooleanValue;
65 import org.projectfloodlight.openflow.types.OFBufferId;
66 import org.projectfloodlight.openflow.types.OFGroup;
67 import org.projectfloodlight.openflow.types.OFPort;
68 import org.projectfloodlight.openflow.types.OFVlanVidMatch;
69 import org.projectfloodlight.openflow.types.TableId;
70 import org.projectfloodlight.openflow.types.TransportPort;
71 import org.projectfloodlight.openflow.types.U32;
72 import org.projectfloodlight.openflow.types.U64;
73 import org.projectfloodlight.openflow.types.VlanPcp;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
77 import java.util.Collections;
78 import java.util.LinkedList;
79 import java.util.List;
80 import java.util.Optional;
83 * Flow mod builder for OpenFlow 1.3+.
85 public class FlowModBuilderVer13 extends FlowModBuilder {
87 private final Logger log = LoggerFactory.getLogger(getClass());
88 private static final int OFPCML_NO_BUFFER = 0xffff;
90 private final TrafficTreatment treatment;
93 * Constructor for a flow mod builder for OpenFlow 1.3.
95 * @param flowRule the flow rule to transform into a flow mod
96 * @param factory the OpenFlow factory to use to build the flow mod
97 * @param xid the transaction ID
99 protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid) {
100 super(flowRule, factory, xid);
102 this.treatment = flowRule.treatment();
106 public OFFlowAdd buildFlowAdd() {
107 Match match = buildMatch();
108 List<OFAction> deferredActions = buildActions(treatment.deferred());
109 List<OFAction> immediateActions = buildActions(treatment.immediate());
110 List<OFInstruction> instructions = Lists.newLinkedList();
113 if (treatment.clearedDeferred()) {
114 instructions.add(factory().instructions().clearActions());
116 if (immediateActions.size() > 0) {
117 instructions.add(factory().instructions().applyActions(immediateActions));
119 if (deferredActions.size() > 0) {
120 instructions.add(factory().instructions().writeActions(deferredActions));
122 if (treatment.tableTransition() != null) {
123 instructions.add(buildTableGoto(treatment.tableTransition()));
125 if (treatment.writeMetadata() != null) {
126 instructions.add(buildMetadata(treatment.writeMetadata()));
128 if (treatment.metered() != null) {
129 instructions.add(buildMeter(treatment.metered()));
132 long cookie = flowRule().id().value();
134 OFFlowAdd fm = factory().buildFlowAdd()
136 .setCookie(U64.of(cookie))
137 .setBufferId(OFBufferId.NO_BUFFER)
138 .setInstructions(instructions)
140 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
141 .setPriority(flowRule().priority())
142 .setTableId(TableId.of(flowRule().tableId()))
149 public OFFlowMod buildFlowMod() {
150 Match match = buildMatch();
151 List<OFAction> deferredActions = buildActions(treatment.deferred());
152 List<OFAction> immediateActions = buildActions(treatment.immediate());
153 List<OFInstruction> instructions = Lists.newLinkedList();
156 if (immediateActions.size() > 0) {
157 instructions.add(factory().instructions().applyActions(immediateActions));
159 if (treatment.clearedDeferred()) {
160 instructions.add(factory().instructions().clearActions());
162 if (deferredActions.size() > 0) {
163 instructions.add(factory().instructions().writeActions(deferredActions));
165 if (treatment.tableTransition() != null) {
166 instructions.add(buildTableGoto(treatment.tableTransition()));
168 if (treatment.writeMetadata() != null) {
169 instructions.add(buildMetadata(treatment.writeMetadata()));
171 if (treatment.metered() != null) {
172 instructions.add(buildMeter(treatment.metered()));
175 long cookie = flowRule().id().value();
177 OFFlowMod fm = factory().buildFlowModify()
179 .setCookie(U64.of(cookie))
180 .setBufferId(OFBufferId.NO_BUFFER)
181 .setInstructions(instructions)
183 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
184 .setPriority(flowRule().priority())
185 .setTableId(TableId.of(flowRule().tableId()))
192 public OFFlowDelete buildFlowDel() {
193 Match match = buildMatch();
195 long cookie = flowRule().id().value();
197 OFFlowDelete fm = factory().buildFlowDelete()
199 .setCookie(U64.of(cookie))
200 .setBufferId(OFBufferId.NO_BUFFER)
202 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
203 .setPriority(flowRule().priority())
204 .setTableId(TableId.of(flowRule().tableId()))
210 private List<OFAction> buildActions(List<Instruction> treatments) {
211 if (treatment == null) {
212 return Collections.emptyList();
215 boolean tableFound = false;
216 List<OFAction> actions = new LinkedList<>();
217 for (Instruction i : treatments) {
221 return Collections.emptyList();
223 actions.add(buildL0Modification(i));
226 actions.add(buildL2Modification(i));
229 actions.add(buildL3Modification(i));
232 actions.add(buildL4Modification(i));
235 OutputInstruction out = (OutputInstruction) i;
236 OFActionOutput.Builder action = factory().actions().buildOutput()
237 .setPort(OFPort.of((int) out.port().toLong()));
238 if (out.port().equals(PortNumber.CONTROLLER)) {
239 action.setMaxLen(OFPCML_NO_BUFFER);
241 actions.add(action.build());
244 GroupInstruction group = (GroupInstruction) i;
245 OFActionGroup.Builder groupBuilder = factory().actions().buildGroup()
246 .setGroup(OFGroup.of(group.groupId().id()));
247 actions.add(groupBuilder.build());
250 SetQueueInstruction queue = (SetQueueInstruction) i;
251 OFActionSetQueue.Builder queueBuilder = factory().actions().buildSetQueue()
252 .setQueueId(queue.queueId());
253 actions.add(queueBuilder.build());
256 //FIXME: should not occur here.
260 log.warn("Instruction type {} not yet implemented.", i.type());
263 if (tableFound && actions.isEmpty()) {
264 // handles the case where there are no actions, but there is
265 // a goto instruction for the next table
266 return Collections.emptyList();
271 private OFInstruction buildTableGoto(Instructions.TableTypeTransition i) {
272 OFInstruction instruction = factory().instructions().gotoTable(
273 TableId.of(i.tableId()));
277 private OFInstruction buildMetadata(Instructions.MetadataInstruction m) {
278 OFInstruction instruction = factory().instructions().writeMetadata(
279 U64.of(m.metadata()), U64.of(m.metadataMask()));
283 private OFInstruction buildMeter(Instructions.MeterInstruction metered) {
284 return factory().instructions().meter(metered.meterId().id());
288 private OFAction buildL0Modification(Instruction i) {
289 L0ModificationInstruction l0m = (L0ModificationInstruction) i;
290 switch (l0m.subtype()) {
292 return buildModLambdaInstruction((ModLambdaInstruction) i);
295 return buildModOchSignalInstruction((ModOchSignalInstruction) i);
296 } catch (NoMappingFoundException e) {
297 log.warn(e.getMessage());
301 log.warn("Unimplemented action type {}.", l0m.subtype());
307 private OFAction buildModLambdaInstruction(ModLambdaInstruction instruction) {
308 return factory().actions().circuit(factory().oxms().ochSigidBasic(
309 new CircuitSignalID((byte) 1, (byte) 2, instruction.lambda(), (short) 1)));
312 private OFAction buildModOchSignalInstruction(ModOchSignalInstruction instruction) {
313 OchSignal signal = instruction.lambda();
314 byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
315 byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
317 return factory().actions().circuit(factory().oxms().ochSigidBasic(
318 new CircuitSignalID(gridType, channelSpacing,
319 (short) signal.spacingMultiplier(), (short) signal.slotGranularity())
323 private OFAction buildL2Modification(Instruction i) {
324 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
325 ModEtherInstruction eth;
327 switch (l2m.subtype()) {
329 eth = (ModEtherInstruction) l2m;
330 oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong()));
333 eth = (ModEtherInstruction) l2m;
334 oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong()));
337 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
338 oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort()));
341 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
342 oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
345 PushHeaderInstructions pushHeaderInstructions =
346 (PushHeaderInstructions) l2m;
347 return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
348 .ethernetType().toShort()));
350 PushHeaderInstructions popHeaderInstructions =
351 (PushHeaderInstructions) l2m;
352 return factory().actions().popMpls(EthType.of(popHeaderInstructions
353 .ethernetType().toShort()));
355 ModMplsLabelInstruction mplsLabel =
356 (ModMplsLabelInstruction) l2m;
357 oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.mplsLabel().toInt()));
360 ModMplsBosInstruction mplsBos = (ModMplsBosInstruction) l2m;
361 oxm = factory().oxms()
362 .mplsBos(mplsBos.mplsBos() ? OFBooleanValue.TRUE
363 : OFBooleanValue.FALSE);
366 return factory().actions().decMplsTtl();
368 return factory().actions().popVlan();
370 PushHeaderInstructions pushVlanInstruction = (PushHeaderInstructions) l2m;
371 return factory().actions().pushVlan(
372 EthType.of(pushVlanInstruction.ethernetType().toShort()));
374 ModTunnelIdInstruction tunnelId = (ModTunnelIdInstruction) l2m;
375 oxm = factory().oxms().tunnelId(U64.of(tunnelId.tunnelId()));
378 log.warn("Unimplemented action type {}.", l2m.subtype());
383 return factory().actions().buildSetField().setField(oxm).build();
388 private OFAction buildL3Modification(Instruction i) {
389 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
394 switch (l3m.subtype()) {
396 ip = (ModIPInstruction) i;
397 ip4 = ip.ip().getIp4Address();
398 oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt()));
401 ip = (ModIPInstruction) i;
402 ip4 = ip.ip().getIp4Address();
403 oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt()));
406 ip = (ModIPInstruction) i;
407 ip6 = ip.ip().getIp6Address();
408 oxm = factory().oxms().ipv6Src(IPv6Address.of(ip6.toOctets()));
411 ip = (ModIPInstruction) i;
412 ip6 = ip.ip().getIp6Address();
413 oxm = factory().oxms().ipv6Dst(IPv6Address.of(ip6.toOctets()));
416 ModIPv6FlowLabelInstruction flowLabelInstruction =
417 (ModIPv6FlowLabelInstruction) i;
418 int flowLabel = flowLabelInstruction.flowLabel();
419 oxm = factory().oxms().ipv6Flabel(IPv6FlowLabel.of(flowLabel));
422 return factory().actions().decNwTtl();
424 return factory().actions().copyTtlIn();
426 return factory().actions().copyTtlOut();
428 log.warn("Unimplemented action type {}.", l3m.subtype());
433 return factory().actions().buildSetField().setField(oxm).build();
438 private OFAction buildL4Modification(Instruction i) {
439 L4ModificationInstruction l4m = (L4ModificationInstruction) i;
440 ModTransportPortInstruction tp;
442 switch (l4m.subtype()) {
444 tp = (ModTransportPortInstruction) l4m;
445 oxm = factory().oxms().tcpSrc(TransportPort.of(tp.port().toInt()));
448 tp = (ModTransportPortInstruction) l4m;
449 oxm = factory().oxms().tcpDst(TransportPort.of(tp.port().toInt()));
452 tp = (ModTransportPortInstruction) l4m;
453 oxm = factory().oxms().udpSrc(TransportPort.of(tp.port().toInt()));
456 tp = (ModTransportPortInstruction) l4m;
457 oxm = factory().oxms().udpDst(TransportPort.of(tp.port().toInt()));
460 log.warn("Unimplemented action type {}.", l4m.subtype());
465 return factory().actions().buildSetField().setField(oxm).build();