8918d3376fc83298644c80b0fe7372d9ae082778
[onosfw.git] /
1 /*
2  * Copyright 2014-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  */
16 package org.onosproject.provider.of.flow.impl;
17
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.L0ModificationInstruction;
30 import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
31 import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction;
32 import org.onosproject.net.flow.instructions.L2ModificationInstruction;
33 import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
34 import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsBosInstruction;
35 import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction;
36 import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
37 import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
38 import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions;
39 import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModTunnelIdInstruction;
40 import org.onosproject.net.flow.instructions.L3ModificationInstruction;
41 import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
42 import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
43 import org.onosproject.net.flow.instructions.L4ModificationInstruction;
44 import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction;
45 import org.projectfloodlight.openflow.protocol.OFFactory;
46 import org.projectfloodlight.openflow.protocol.OFFlowAdd;
47 import org.projectfloodlight.openflow.protocol.OFFlowDelete;
48 import org.projectfloodlight.openflow.protocol.OFFlowMod;
49 import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
50 import org.projectfloodlight.openflow.protocol.action.OFAction;
51 import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
52 import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
53 import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
54 import org.projectfloodlight.openflow.protocol.match.Match;
55 import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
56 import org.projectfloodlight.openflow.types.CircuitSignalID;
57 import org.projectfloodlight.openflow.types.EthType;
58 import org.projectfloodlight.openflow.types.IPv4Address;
59 import org.projectfloodlight.openflow.types.IPv6Address;
60 import org.projectfloodlight.openflow.types.IPv6FlowLabel;
61 import org.projectfloodlight.openflow.types.MacAddress;
62 import org.projectfloodlight.openflow.types.OFBooleanValue;
63 import org.projectfloodlight.openflow.types.OFBufferId;
64 import org.projectfloodlight.openflow.types.OFGroup;
65 import org.projectfloodlight.openflow.types.OFPort;
66 import org.projectfloodlight.openflow.types.OFVlanVidMatch;
67 import org.projectfloodlight.openflow.types.TableId;
68 import org.projectfloodlight.openflow.types.TransportPort;
69 import org.projectfloodlight.openflow.types.U32;
70 import org.projectfloodlight.openflow.types.U64;
71 import org.projectfloodlight.openflow.types.VlanPcp;
72 import org.slf4j.Logger;
73 import org.slf4j.LoggerFactory;
74
75 import java.util.Collections;
76 import java.util.LinkedList;
77 import java.util.List;
78 import java.util.Optional;
79
80 /**
81  * Flow mod builder for OpenFlow 1.3+.
82  */
83 public class FlowModBuilderVer13 extends FlowModBuilder {
84
85     private final Logger log = LoggerFactory.getLogger(getClass());
86     private static final int OFPCML_NO_BUFFER = 0xffff;
87
88     private final TrafficTreatment treatment;
89
90     /**
91      * Constructor for a flow mod builder for OpenFlow 1.3.
92      *
93      * @param flowRule the flow rule to transform into a flow mod
94      * @param factory the OpenFlow factory to use to build the flow mod
95      * @param xid the transaction ID
96      */
97     protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid) {
98         super(flowRule, factory, xid);
99
100         this.treatment = flowRule.treatment();
101     }
102
103     @Override
104     public OFFlowAdd buildFlowAdd() {
105         Match match = buildMatch();
106         List<OFAction> deferredActions = buildActions(treatment.deferred());
107         List<OFAction> immediateActions = buildActions(treatment.immediate());
108         List<OFInstruction> instructions = Lists.newLinkedList();
109
110
111         if (treatment.clearedDeferred()) {
112             instructions.add(factory().instructions().clearActions());
113         }
114         if (immediateActions.size() > 0) {
115             instructions.add(factory().instructions().applyActions(immediateActions));
116         }
117         if (deferredActions.size() > 0) {
118             instructions.add(factory().instructions().writeActions(deferredActions));
119         }
120         if (treatment.tableTransition() != null) {
121             instructions.add(buildTableGoto(treatment.tableTransition()));
122         }
123         if (treatment.writeMetadata() != null) {
124             instructions.add(buildMetadata(treatment.writeMetadata()));
125         }
126
127         long cookie = flowRule().id().value();
128
129         OFFlowAdd fm = factory().buildFlowAdd()
130                 .setXid(xid)
131                 .setCookie(U64.of(cookie))
132                 .setBufferId(OFBufferId.NO_BUFFER)
133                 .setInstructions(instructions)
134                 .setMatch(match)
135                 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
136                 .setPriority(flowRule().priority())
137                 .setTableId(TableId.of(flowRule().tableId()))
138                 .build();
139
140         return fm;
141     }
142
143     @Override
144     public OFFlowMod buildFlowMod() {
145         Match match = buildMatch();
146         List<OFAction> deferredActions = buildActions(treatment.deferred());
147         List<OFAction> immediateActions = buildActions(treatment.immediate());
148         List<OFInstruction> instructions = Lists.newLinkedList();
149
150
151         if (immediateActions.size() > 0) {
152             instructions.add(factory().instructions().applyActions(immediateActions));
153         }
154         if (treatment.clearedDeferred()) {
155             instructions.add(factory().instructions().clearActions());
156         }
157         if (deferredActions.size() > 0) {
158             instructions.add(factory().instructions().writeActions(deferredActions));
159         }
160         if (treatment.tableTransition() != null) {
161             instructions.add(buildTableGoto(treatment.tableTransition()));
162         }
163         if (treatment.writeMetadata() != null) {
164             instructions.add(buildMetadata(treatment.writeMetadata()));
165         }
166         if (treatment.metered() != null) {
167             instructions.add(buildMeter(treatment.metered()));
168         }
169
170         long cookie = flowRule().id().value();
171
172         OFFlowMod fm = factory().buildFlowModify()
173                 .setXid(xid)
174                 .setCookie(U64.of(cookie))
175                 .setBufferId(OFBufferId.NO_BUFFER)
176                 .setInstructions(instructions)
177                 .setMatch(match)
178                 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
179                 .setPriority(flowRule().priority())
180                 .setTableId(TableId.of(flowRule().tableId()))
181                 .build();
182
183         return fm;
184     }
185
186     @Override
187     public OFFlowDelete buildFlowDel() {
188         Match match = buildMatch();
189
190         long cookie = flowRule().id().value();
191
192         OFFlowDelete fm = factory().buildFlowDelete()
193                 .setXid(xid)
194                 .setCookie(U64.of(cookie))
195                 .setBufferId(OFBufferId.NO_BUFFER)
196                 .setMatch(match)
197                 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
198                 .setPriority(flowRule().priority())
199                 .setTableId(TableId.of(flowRule().tableId()))
200                 .build();
201
202         return fm;
203     }
204
205     private List<OFAction> buildActions(List<Instruction> treatments) {
206         if (treatment == null) {
207             return Collections.emptyList();
208         }
209
210         boolean tableFound = false;
211         List<OFAction> actions = new LinkedList<>();
212         for (Instruction i : treatments) {
213             switch (i.type()) {
214                 case DROP:
215                     return Collections.emptyList();
216                 case L0MODIFICATION:
217                     actions.add(buildL0Modification(i));
218                     break;
219                 case L2MODIFICATION:
220                     actions.add(buildL2Modification(i));
221                     break;
222                 case L3MODIFICATION:
223                     actions.add(buildL3Modification(i));
224                     break;
225                 case L4MODIFICATION:
226                     actions.add(buildL4Modification(i));
227                     break;
228                 case OUTPUT:
229                     OutputInstruction out = (OutputInstruction) i;
230                     OFActionOutput.Builder action = factory().actions().buildOutput()
231                             .setPort(OFPort.of((int) out.port().toLong()));
232                     if (out.port().equals(PortNumber.CONTROLLER)) {
233                         action.setMaxLen(OFPCML_NO_BUFFER);
234                     }
235                     actions.add(action.build());
236                     break;
237                 case GROUP:
238                     GroupInstruction group = (GroupInstruction) i;
239                     OFActionGroup.Builder groupBuilder = factory().actions().buildGroup()
240                             .setGroup(OFGroup.of(group.groupId().id()));
241                     actions.add(groupBuilder.build());
242                     break;
243                 case TABLE:
244                     //FIXME: should not occur here.
245                     tableFound = true;
246                     break;
247                 default:
248                     log.warn("Instruction type {} not yet implemented.", i.type());
249             }
250         }
251         if (tableFound && actions.isEmpty()) {
252             // handles the case where there are no actions, but there is
253             // a goto instruction for the next table
254             return Collections.emptyList();
255         }
256         return actions;
257     }
258
259     private OFInstruction buildTableGoto(Instructions.TableTypeTransition i) {
260         OFInstruction instruction = factory().instructions().gotoTable(
261                 TableId.of(i.tableId()));
262         return instruction;
263     }
264
265     private OFInstruction buildMetadata(Instructions.MetadataInstruction m) {
266         OFInstruction instruction = factory().instructions().writeMetadata(
267                 U64.of(m.metadata()), U64.of(m.metadataMask()));
268         return instruction;
269     }
270
271     private OFInstruction buildMeter(Instructions.MeterInstruction metered) {
272         return factory().instructions().meter(metered.meterId().id());
273     }
274
275
276     private OFAction buildL0Modification(Instruction i) {
277         L0ModificationInstruction l0m = (L0ModificationInstruction) i;
278         switch (l0m.subtype()) {
279             case LAMBDA:
280                 return buildModLambdaInstruction((ModLambdaInstruction) i);
281             case OCH:
282                 try {
283                     return buildModOchSignalInstruction((ModOchSignalInstruction) i);
284                 } catch (NoMappingFoundException e) {
285                     log.warn(e.getMessage());
286                     break;
287                 }
288             default:
289                 log.warn("Unimplemented action type {}.", l0m.subtype());
290                 break;
291         }
292         return null;
293     }
294
295     private OFAction buildModLambdaInstruction(ModLambdaInstruction instruction) {
296         return factory().actions().circuit(factory().oxms().ochSigidBasic(
297                 new CircuitSignalID((byte) 1, (byte) 2, instruction.lambda(), (short) 1)));
298     }
299
300     private OFAction buildModOchSignalInstruction(ModOchSignalInstruction instruction) {
301         OchSignal signal = instruction.lambda();
302         byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
303         byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
304
305         return factory().actions().circuit(factory().oxms().ochSigidBasic(
306                 new CircuitSignalID(gridType, channelSpacing,
307                         (short) signal.spacingMultiplier(), (short) signal.slotGranularity())
308         ));
309     }
310
311     private OFAction buildL2Modification(Instruction i) {
312         L2ModificationInstruction l2m = (L2ModificationInstruction) i;
313         ModEtherInstruction eth;
314         OFOxm<?> oxm = null;
315         switch (l2m.subtype()) {
316             case ETH_DST:
317                 eth = (ModEtherInstruction) l2m;
318                 oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong()));
319                 break;
320             case ETH_SRC:
321                 eth = (ModEtherInstruction) l2m;
322                 oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong()));
323                 break;
324             case VLAN_ID:
325                 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
326                 oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort()));
327                 break;
328             case VLAN_PCP:
329                 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
330                 oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
331                 break;
332             case MPLS_PUSH:
333                 PushHeaderInstructions pushHeaderInstructions =
334                         (PushHeaderInstructions) l2m;
335                 return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
336                                                                .ethernetType().toShort()));
337             case MPLS_POP:
338                 PushHeaderInstructions popHeaderInstructions =
339                         (PushHeaderInstructions) l2m;
340                 return factory().actions().popMpls(EthType.of(popHeaderInstructions
341                                                               .ethernetType().toShort()));
342             case MPLS_LABEL:
343                 ModMplsLabelInstruction mplsLabel =
344                         (ModMplsLabelInstruction) l2m;
345                 oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.mplsLabel().toInt()));
346                 break;
347             case MPLS_BOS:
348                 ModMplsBosInstruction mplsBos = (ModMplsBosInstruction) l2m;
349                 oxm = factory().oxms()
350                         .mplsBos(mplsBos.mplsBos() ? OFBooleanValue.TRUE
351                                                    : OFBooleanValue.FALSE);
352                 break;
353             case DEC_MPLS_TTL:
354                 return factory().actions().decMplsTtl();
355             case VLAN_POP:
356                 return factory().actions().popVlan();
357             case VLAN_PUSH:
358                 PushHeaderInstructions pushVlanInstruction = (PushHeaderInstructions) l2m;
359                 return factory().actions().pushVlan(
360                         EthType.of(pushVlanInstruction.ethernetType().toShort()));
361             case TUNNEL_ID:
362                 ModTunnelIdInstruction tunnelId = (ModTunnelIdInstruction) l2m;
363                 oxm = factory().oxms().tunnelId(U64.of(tunnelId.tunnelId()));
364                 break;
365             default:
366                 log.warn("Unimplemented action type {}.", l2m.subtype());
367                 break;
368         }
369
370         if (oxm != null) {
371             return factory().actions().buildSetField().setField(oxm).build();
372         }
373         return null;
374     }
375
376     private OFAction buildL3Modification(Instruction i) {
377         L3ModificationInstruction l3m = (L3ModificationInstruction) i;
378         ModIPInstruction ip;
379         Ip4Address ip4;
380         Ip6Address ip6;
381         OFOxm<?> oxm = null;
382         switch (l3m.subtype()) {
383             case IPV4_SRC:
384                 ip = (ModIPInstruction) i;
385                 ip4 = ip.ip().getIp4Address();
386                 oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt()));
387                 break;
388             case IPV4_DST:
389                 ip = (ModIPInstruction) i;
390                 ip4 = ip.ip().getIp4Address();
391                 oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt()));
392                 break;
393             case IPV6_SRC:
394                 ip = (ModIPInstruction) i;
395                 ip6 = ip.ip().getIp6Address();
396                 oxm = factory().oxms().ipv6Src(IPv6Address.of(ip6.toOctets()));
397                 break;
398             case IPV6_DST:
399                 ip = (ModIPInstruction) i;
400                 ip6 = ip.ip().getIp6Address();
401                 oxm = factory().oxms().ipv6Dst(IPv6Address.of(ip6.toOctets()));
402                 break;
403             case IPV6_FLABEL:
404                 ModIPv6FlowLabelInstruction flowLabelInstruction =
405                         (ModIPv6FlowLabelInstruction) i;
406                 int flowLabel = flowLabelInstruction.flowLabel();
407                 oxm = factory().oxms().ipv6Flabel(IPv6FlowLabel.of(flowLabel));
408                 break;
409             case DEC_TTL:
410                 return factory().actions().decNwTtl();
411             case TTL_IN:
412                 return factory().actions().copyTtlIn();
413             case TTL_OUT:
414                 return factory().actions().copyTtlOut();
415             default:
416                 log.warn("Unimplemented action type {}.", l3m.subtype());
417                 break;
418         }
419
420         if (oxm != null) {
421             return factory().actions().buildSetField().setField(oxm).build();
422         }
423         return null;
424     }
425
426     private OFAction buildL4Modification(Instruction i) {
427         L4ModificationInstruction l4m = (L4ModificationInstruction) i;
428         ModTransportPortInstruction tp;
429         OFOxm<?> oxm = null;
430         switch (l4m.subtype()) {
431             case TCP_SRC:
432                 tp = (ModTransportPortInstruction) l4m;
433                 oxm = factory().oxms().tcpSrc(TransportPort.of(tp.port().toInt()));
434                 break;
435             case TCP_DST:
436                 tp = (ModTransportPortInstruction) l4m;
437                 oxm = factory().oxms().tcpDst(TransportPort.of(tp.port().toInt()));
438                 break;
439             case UDP_SRC:
440                 tp = (ModTransportPortInstruction) l4m;
441                 oxm = factory().oxms().udpSrc(TransportPort.of(tp.port().toInt()));
442                 break;
443             case UDP_DST:
444                 tp = (ModTransportPortInstruction) l4m;
445                 oxm = factory().oxms().udpDst(TransportPort.of(tp.port().toInt()));
446                 break;
447             default:
448                 log.warn("Unimplemented action type {}.", l4m.subtype());
449                 break;
450         }
451
452         if (oxm != null) {
453             return factory().actions().buildSetField().setField(oxm).build();
454         }
455         return null;
456     }
457
458 }