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 static org.onosproject.net.flow.criteria.Criteria.matchLambda;
19 import static org.onosproject.net.flow.criteria.Criteria.matchOchSignalType;
20 import static org.onosproject.provider.of.flow.impl.OpenFlowValueMapper.lookupChannelSpacing;
21 import static org.onosproject.provider.of.flow.impl.OpenFlowValueMapper.lookupGridType;
22 import static org.onosproject.provider.of.flow.impl.OpenFlowValueMapper.lookupOchSignalType;
23 import static org.slf4j.LoggerFactory.getLogger;
25 import java.util.List;
27 import org.onlab.packet.Ip4Address;
28 import org.onlab.packet.Ip4Prefix;
29 import org.onlab.packet.Ip6Address;
30 import org.onlab.packet.Ip6Prefix;
31 import org.onlab.packet.MacAddress;
32 import org.onlab.packet.MplsLabel;
33 import org.onlab.packet.TpPort;
34 import org.onlab.packet.VlanId;
35 import org.onosproject.core.DefaultGroupId;
36 import org.onosproject.net.DeviceId;
37 import org.onosproject.net.Lambda;
38 import org.onosproject.net.PortNumber;
39 import org.onosproject.net.flow.DefaultFlowEntry;
40 import org.onosproject.net.flow.DefaultFlowRule;
41 import org.onosproject.net.flow.DefaultTrafficSelector;
42 import org.onosproject.net.flow.DefaultTrafficTreatment;
43 import org.onosproject.net.flow.FlowEntry;
44 import org.onosproject.net.flow.FlowEntry.FlowEntryState;
45 import org.onosproject.net.flow.FlowRule;
46 import org.onosproject.net.flow.TrafficSelector;
47 import org.onosproject.net.flow.TrafficTreatment;
48 import org.onosproject.net.flow.instructions.Instructions;
49 import org.onosproject.openflow.controller.Dpid;
50 import org.projectfloodlight.openflow.protocol.OFFlowMod;
51 import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
52 import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
53 import org.projectfloodlight.openflow.protocol.action.OFAction;
54 import org.projectfloodlight.openflow.protocol.action.OFActionCircuit;
55 import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
56 import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
57 import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
58 import org.projectfloodlight.openflow.protocol.action.OFActionPopMpls;
59 import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
60 import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
61 import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
62 import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
63 import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
64 import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
65 import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
66 import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
67 import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
68 import org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable;
69 import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions;
70 import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteMetadata;
71 import org.projectfloodlight.openflow.protocol.match.Match;
72 import org.projectfloodlight.openflow.protocol.match.MatchField;
73 import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
74 import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigidBasic;
75 import org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13;
76 import org.projectfloodlight.openflow.types.CircuitSignalID;
77 import org.projectfloodlight.openflow.types.EthType;
78 import org.projectfloodlight.openflow.types.IPv4Address;
79 import org.projectfloodlight.openflow.types.IPv6Address;
80 import org.projectfloodlight.openflow.types.Masked;
81 import org.projectfloodlight.openflow.types.OFVlanVidMatch;
82 import org.projectfloodlight.openflow.types.TransportPort;
83 import org.projectfloodlight.openflow.types.U32;
84 import org.projectfloodlight.openflow.types.U64;
85 import org.projectfloodlight.openflow.types.U8;
86 import org.projectfloodlight.openflow.types.VlanPcp;
87 import org.slf4j.Logger;
89 import com.google.common.collect.Lists;
91 public class FlowEntryBuilder {
92 private final Logger log = getLogger(getClass());
94 private final OFFlowStatsEntry stat;
95 private final OFFlowRemoved removed;
96 private final OFFlowMod flowMod;
98 private final Match match;
100 // All actions are contained in an OFInstruction. For OF1.0
101 // the instruction type is apply instruction (immediate set in ONOS speak)
102 private final List<OFInstruction> instructions;
104 private final Dpid dpid;
106 public enum FlowType { STAT, REMOVED, MOD }
108 private final FlowType type;
110 public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry) {
112 this.match = entry.getMatch();
113 this.instructions = getInstructions(entry);
117 this.type = FlowType.STAT;
120 public FlowEntryBuilder(Dpid dpid, OFFlowRemoved removed) {
121 this.match = removed.getMatch();
122 this.removed = removed;
125 this.instructions = null;
128 this.type = FlowType.REMOVED;
132 public FlowEntryBuilder(Dpid dpid, OFFlowMod fm) {
133 this.match = fm.getMatch();
135 this.instructions = getInstructions(fm);
136 this.type = FlowType.MOD;
142 public FlowEntry build(FlowEntryState... state) {
146 rule = DefaultFlowRule.builder()
147 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
148 .withSelector(buildSelector())
149 .withTreatment(buildTreatment())
150 .withPriority(stat.getPriority())
151 .makeTemporary(stat.getIdleTimeout())
152 .withCookie(stat.getCookie().getValue())
153 .forTable(stat.getTableId().getValue())
156 return new DefaultFlowEntry(rule, FlowEntryState.ADDED,
157 stat.getDurationSec(), stat.getPacketCount().getValue(),
158 stat.getByteCount().getValue());
160 rule = DefaultFlowRule.builder()
161 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
162 .withSelector(buildSelector())
163 .withPriority(removed.getPriority())
164 .makeTemporary(removed.getIdleTimeout())
165 .withCookie(removed.getCookie().getValue())
166 .forTable(removed.getTableId().getValue())
169 return new DefaultFlowEntry(rule, FlowEntryState.REMOVED, removed.getDurationSec(),
170 removed.getPacketCount().getValue(), removed.getByteCount().getValue());
172 FlowEntryState flowState = state.length > 0 ? state[0] : FlowEntryState.FAILED;
173 rule = DefaultFlowRule.builder()
174 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
175 .withSelector(buildSelector())
176 .withTreatment(buildTreatment())
177 .withPriority(flowMod.getPriority())
178 .makeTemporary(flowMod.getIdleTimeout())
179 .withCookie(flowMod.getCookie().getValue())
180 .forTable(flowMod.getTableId().getValue())
183 return new DefaultFlowEntry(rule, flowState, 0, 0, 0);
185 log.error("Unknown flow type : {}", this.type);
191 private List<OFInstruction> getInstructions(OFFlowMod entry) {
192 switch (entry.getVersion()) {
194 return Lists.newArrayList(OFFactoryVer13.INSTANCE.instructions()
196 entry.getActions()));
200 return entry.getInstructions();
202 log.warn("Unknown OF version {}", entry.getVersion());
204 return Lists.newLinkedList();
207 private List<OFInstruction> getInstructions(OFFlowStatsEntry entry) {
208 switch (entry.getVersion()) {
210 return Lists.newArrayList(
211 OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions()));
215 return entry.getInstructions();
217 log.warn("Unknown OF version {}", entry.getVersion());
219 return Lists.newLinkedList();
222 private TrafficTreatment buildTreatment() {
223 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
224 // If this is a drop rule
225 if (instructions.size() == 0) {
227 return builder.build();
229 for (OFInstruction in : instructions) {
230 switch (in.getType()) {
232 builder.transition(((int) ((OFInstructionGotoTable) in)
233 .getTableId().getValue()));
236 OFInstructionWriteMetadata m = (OFInstructionWriteMetadata) in;
237 builder.writeMetadata(m.getMetadata().getValue(),
238 m.getMetadataMask().getValue());
242 buildActions(((OFInstructionWriteActions) in).getActions(),
247 buildActions(((OFInstructionApplyActions) in).getActions(),
251 builder.wipeDeferred();
258 log.warn("Unknown instructions type {}", in.getType());
262 return builder.build();
265 private TrafficTreatment.Builder buildActions(List<OFAction> actions,
266 TrafficTreatment.Builder builder) {
267 for (OFAction act : actions) {
268 switch (act.getType()) {
270 OFActionOutput out = (OFActionOutput) act;
272 PortNumber.portNumber(out.getPort().getPortNumber()));
275 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
276 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
279 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
280 builder.setVlanPcp(pcp.getVlanPcp().getValue());
283 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
285 MacAddress.valueOf(dldst.getDlAddr().getLong()));
288 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
290 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
294 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
295 IPv4Address di = nwdst.getNwAddr();
296 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
299 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
300 IPv4Address si = nwsrc.getNwAddr();
301 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
304 OFActionExperimenter exp = (OFActionExperimenter) act;
305 if (exp.getExperimenter() == 0x80005A06 ||
306 exp.getExperimenter() == 0x748771) {
307 OFActionCircuit ct = (OFActionCircuit) exp;
308 short lambda = ((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber();
309 builder.add(Instructions.modL0Lambda(Lambda.indexedLambda(lambda)));
311 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
315 OFActionSetField setField = (OFActionSetField) act;
316 handleSetField(builder, setField.getField());
319 OFActionPopMpls popMpls = (OFActionPopMpls) act;
320 builder.popMpls((short) popMpls.getEthertype().getValue());
329 builder.copyTtlOut();
332 builder.decMplsTtl();
338 OFActionGroup group = (OFActionGroup) act;
339 builder.group(new DefaultGroupId(group.getGroup().getGroupNumber()));
362 log.warn("Action type {} not yet implemented.", act.getType());
369 private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) {
370 switch (oxm.getMatchField().id) {
372 @SuppressWarnings("unchecked")
373 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
374 builder.setVlanPcp(vlanpcp.getValue().getValue());
377 @SuppressWarnings("unchecked")
378 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
379 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
382 @SuppressWarnings("unchecked")
383 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
384 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
385 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
388 @SuppressWarnings("unchecked")
389 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
390 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
391 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
394 @SuppressWarnings("unchecked")
395 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
396 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
399 @SuppressWarnings("unchecked")
400 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
401 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
404 @SuppressWarnings("unchecked")
405 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
406 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
409 @SuppressWarnings("unchecked")
410 OFOxm<U8> mplsBos = (OFOxm<U8>) oxm;
411 builder.setMplsBos(mplsBos.getValue() == U8.ZERO ? false : true);
414 @SuppressWarnings("unchecked")
415 OFOxm<U64> tunnelId = (OFOxm<U64>) oxm;
416 builder.setTunnelId(tunnelId.getValue().getValue());
419 @SuppressWarnings("unchecked")
420 OFOxm<TransportPort> tcpdst = (OFOxm<TransportPort>) oxm;
421 builder.setTcpDst(TpPort.tpPort(tcpdst.getValue().getPort()));
424 @SuppressWarnings("unchecked")
425 OFOxm<TransportPort> tcpsrc = (OFOxm<TransportPort>) oxm;
426 builder.setTcpSrc(TpPort.tpPort(tcpsrc.getValue().getPort()));
429 @SuppressWarnings("unchecked")
430 OFOxm<TransportPort> udpdst = (OFOxm<TransportPort>) oxm;
431 builder.setUdpDst(TpPort.tpPort(udpdst.getValue().getPort()));
434 @SuppressWarnings("unchecked")
435 OFOxm<TransportPort> udpsrc = (OFOxm<TransportPort>) oxm;
436 builder.setUdpSrc(TpPort.tpPort(udpsrc.getValue().getPort()));
443 case BSN_EGR_PORT_GROUP_ID:
444 case BSN_GLOBAL_VRF_ALLOWED:
445 case BSN_IN_PORTS_128:
446 case BSN_L3_DST_CLASS_ID:
447 case BSN_L3_INTERFACE_CLASS_ID:
448 case BSN_L3_SRC_CLASS_ID:
459 case BSN_VLAN_XLATE_PORT_GROUP_ID:
480 case OCH_SIGID_BASIC:
482 case OCH_SIGTYPE_BASIC:
486 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
491 // CHECKSTYLE IGNORE MethodLength FOR NEXT 1 LINES
492 private TrafficSelector buildSelector() {
495 Ip6Address ip6Address;
498 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
499 for (MatchField<?> field : match.getMatchFields()) {
502 builder.matchInPort(PortNumber
503 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
506 builder.matchInPhyPort(PortNumber
507 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
511 match.get(MatchField.METADATA).getValue().getValue();
512 builder.matchMetadata(metadata);
515 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
516 builder.matchEthDst(mac);
519 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
520 builder.matchEthSrc(mac);
523 int ethType = match.get(MatchField.ETH_TYPE).getValue();
524 if (ethType == EthType.VLAN_FRAME.getValue()) {
525 builder.matchVlanId(VlanId.ANY);
527 builder.matchEthType((short) ethType);
531 VlanId vlanId = null;
532 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
533 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
534 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
535 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
539 if (!match.get(MatchField.VLAN_VID).isPresentBitSet()) {
540 vlanId = VlanId.NONE;
542 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
545 if (vlanId != null) {
546 builder.matchVlanId(vlanId);
550 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
551 builder.matchVlanPcp(vlanPcp);
554 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
555 builder.matchIPDscp(ipDscp);
558 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
559 builder.matchIPEcn(ipEcn);
562 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
563 builder.matchIPProtocol((byte) proto);
566 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
567 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
568 ip4Prefix = Ip4Prefix.valueOf(
569 maskedIp.getValue().getInt(),
570 maskedIp.getMask().asCidrMaskLength());
572 ip4Prefix = Ip4Prefix.valueOf(
573 match.get(MatchField.IPV4_SRC).getInt(),
574 Ip4Prefix.MAX_MASK_LENGTH);
576 builder.matchIPSrc(ip4Prefix);
579 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
580 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
581 ip4Prefix = Ip4Prefix.valueOf(
582 maskedIp.getValue().getInt(),
583 maskedIp.getMask().asCidrMaskLength());
585 ip4Prefix = Ip4Prefix.valueOf(
586 match.get(MatchField.IPV4_DST).getInt(),
587 Ip4Prefix.MAX_MASK_LENGTH);
589 builder.matchIPDst(ip4Prefix);
592 builder.matchTcpSrc(TpPort.tpPort(match.get(MatchField.TCP_SRC).getPort()));
595 builder.matchTcpDst(TpPort.tpPort(match.get(MatchField.TCP_DST).getPort()));
598 builder.matchUdpSrc(TpPort.tpPort(match.get(MatchField.UDP_SRC).getPort()));
601 builder.matchUdpDst(TpPort.tpPort(match.get(MatchField.UDP_DST).getPort()));
604 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
608 builder.matchMplsBos(match.get(MatchField.MPLS_BOS).getValue());
611 builder.matchSctpSrc(TpPort.tpPort(match.get(MatchField.SCTP_SRC).getPort()));
614 builder.matchSctpDst(TpPort.tpPort(match.get(MatchField.SCTP_DST).getPort()));
617 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
618 builder.matchIcmpType(icmpType);
621 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
622 builder.matchIcmpCode(icmpCode);
625 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
626 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
627 ip6Prefix = Ip6Prefix.valueOf(
628 maskedIp.getValue().getBytes(),
629 maskedIp.getMask().asCidrMaskLength());
631 ip6Prefix = Ip6Prefix.valueOf(
632 match.get(MatchField.IPV6_SRC).getBytes(),
633 Ip6Prefix.MAX_MASK_LENGTH);
635 builder.matchIPv6Src(ip6Prefix);
638 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
639 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
640 ip6Prefix = Ip6Prefix.valueOf(
641 maskedIp.getValue().getBytes(),
642 maskedIp.getMask().asCidrMaskLength());
644 ip6Prefix = Ip6Prefix.valueOf(
645 match.get(MatchField.IPV6_DST).getBytes(),
646 Ip6Prefix.MAX_MASK_LENGTH);
648 builder.matchIPv6Dst(ip6Prefix);
652 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
653 builder.matchIPv6FlowLabel(flowLabel);
656 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
657 builder.matchIcmpv6Type(icmpv6type);
660 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
661 builder.matchIcmpv6Code(icmpv6code);
665 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
666 builder.matchIPv6NDTargetAddress(ip6Address);
669 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
670 builder.matchIPv6NDSourceLinkLayerAddress(mac);
673 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
674 builder.matchIPv6NDTargetLinkLayerAddress(mac);
677 builder.matchIPv6ExthdrFlags((short) match.get(MatchField.IPV6_EXTHDR)
681 CircuitSignalID sigId = match.get(MatchField.OCH_SIGID);
682 builder.add(matchLambda(Lambda.ochSignal(
683 lookupGridType(sigId.getGridType()), lookupChannelSpacing(sigId.getChannelSpacing()),
684 sigId.getChannelNumber(), sigId.getSpectralWidth())
688 U8 sigType = match.get(MatchField.OCH_SIGTYPE);
689 builder.add(matchOchSignalType(lookupOchSignalType((byte) sigType.getValue())));
692 long tunnelId = match.get(MatchField.TUNNEL_ID).getValue();
693 builder.matchTunnelId(tunnelId);
702 log.warn("Match type {} not yet implemented.", field.id);
705 return builder.build();