blob: 923dcdefc67d49470cbcd7052954c37c2c26b586 [file] [log] [blame]
Jonathan Hart3c259162015-10-21 21:31:19 -07001/*
2 * Copyright 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
17package org.onosproject.driver.extensions;
18
19import org.onlab.packet.Ip4Address;
alshabib880b6442015-11-23 22:13:04 -080020import org.onosproject.net.behaviour.ExtensionTreatmentResolver;
Jonathan Hart3c259162015-10-21 21:31:19 -070021import org.onosproject.net.driver.AbstractHandlerBehaviour;
alshabib880b6442015-11-23 22:13:04 -080022import org.onosproject.net.flow.instructions.ExtensionTreatment;
23import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
24import org.onosproject.openflow.controller.ExtensionTreatmentInterpreter;
Jonathan Hart3c259162015-10-21 21:31:19 -070025import org.projectfloodlight.openflow.protocol.OFActionType;
26import org.projectfloodlight.openflow.protocol.OFFactory;
27import org.projectfloodlight.openflow.protocol.action.OFAction;
samueljcc4f4d25a2015-12-07 10:52:28 +080028import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
29import org.projectfloodlight.openflow.protocol.action.OFActionNicira;
30import org.projectfloodlight.openflow.protocol.action.OFActionNiciraMove;
Jonathan Hart3c259162015-10-21 21:31:19 -070031import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
32import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
33import org.projectfloodlight.openflow.protocol.oxm.OFOxmTunnelIpv4Dst;
34import org.projectfloodlight.openflow.types.IPv4Address;
35
36/**
Jonathan Hart26a8d952015-12-02 15:16:35 -080037 * Interpreter for Nicira OpenFlow treatment extensions.
Jonathan Hart3c259162015-10-21 21:31:19 -070038 */
alshabib880b6442015-11-23 22:13:04 -080039public class NiciraExtensionTreatmentInterpreter extends AbstractHandlerBehaviour
40 implements ExtensionTreatmentInterpreter, ExtensionTreatmentResolver {
Jonathan Hart3c259162015-10-21 21:31:19 -070041
samueljcc4f4d25a2015-12-07 10:52:28 +080042 private static final int TYPE_NICIRA = 0x2320;
43 private static final int SUB_TYPE_MOVE = 6;
44 private static final int SRC_ARP_SHA = 0x00012206;
45 private static final int SRC_ARP_SPA = 0x00002004;
46 private static final int SRC_ETH = 0x00000406;
47 private static final int SRC_IP = 0x00000e04;
48
Jonathan Hart3c259162015-10-21 21:31:19 -070049 @Override
alshabib880b6442015-11-23 22:13:04 -080050 public boolean supported(ExtensionTreatmentType extensionTreatmentType) {
51 if (extensionTreatmentType.equals(
52 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type())) {
Jonathan Hart3c259162015-10-21 21:31:19 -070053 return true;
54 }
alshabib880b6442015-11-23 22:13:04 -080055 if (extensionTreatmentType.equals(
56 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT.type())) {
BitOhenryb53ac6c2015-11-16 20:58:58 +080057 return true;
58 }
Phaneendra Mandaab5a7362015-12-02 01:10:01 +053059 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SPI.type())) {
60 return true;
61 }
62 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SI.type())) {
63 return true;
64 }
65 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH1.type())) {
66 return true;
67 }
68 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH2.type())) {
69 return true;
70 }
71 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH3.type())) {
72 return true;
73 }
74 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH4.type())) {
Phaneendra Mandace66cbc2015-11-26 18:07:48 +053075 return true;
76 }
BitOhenry74dd7e12015-12-01 09:07:19 +080077 if (extensionTreatmentType.equals(
78 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT_TABLE.type())) {
79 return true;
80 }
samueljcc4a527ed2015-12-03 13:59:49 +080081 if (extensionTreatmentType.equals(
82 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SHA_TO_THA.type())) {
83 return true;
84 }
85 if (extensionTreatmentType.equals(
86 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SPA_TO_TPA.type())) {
87 return true;
88 }
89 if (extensionTreatmentType.equals(
90 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ETH_SRC_TO_DST.type())) {
91 return true;
92 }
93 if (extensionTreatmentType.equals(
94 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_IP_SRC_TO_DST.type())) {
95 return true;
96 }
Jonathan Hart3c259162015-10-21 21:31:19 -070097 return false;
98 }
99
100 @Override
alshabib880b6442015-11-23 22:13:04 -0800101 public OFAction mapInstruction(OFFactory factory, ExtensionTreatment extensionTreatment) {
102 ExtensionTreatmentType type = extensionTreatment.type();
103 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type())) {
104 NiciraSetTunnelDst tunnelDst = (NiciraSetTunnelDst) extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700105 return factory.actions().setField(factory.oxms().tunnelIpv4Dst(
106 IPv4Address.of(tunnelDst.tunnelDst().toInt())));
107 }
alshabib880b6442015-11-23 22:13:04 -0800108 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT.type())) {
BitOhenry5e2233a2015-12-08 09:26:38 +0800109 NiciraResubmit resubmit = (NiciraResubmit) extensionTreatment;
110 return factory.actions().niciraResubmit((int) resubmit.inPort().toLong(),
111 resubmit.table());
BitOhenryb53ac6c2015-11-16 20:58:58 +0800112 }
BitOhenry74dd7e12015-12-01 09:07:19 +0800113 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT_TABLE.type())) {
BitOhenry5e2233a2015-12-08 09:26:38 +0800114 NiciraResubmitTable resubmitTable = (NiciraResubmitTable) extensionTreatment;
115 return factory.actions().niciraResubmitTable((int) resubmitTable.inPort().toLong(),
116 resubmitTable.table());
Phaneendra Mandaab5a7362015-12-02 01:10:01 +0530117 }
Phaneendra Manda0c0f14b2015-12-09 20:38:27 +0530118 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SPI.type())) {
119 NiciraSetNshSpi niciraNshSpi = (NiciraSetNshSpi) extensionTreatment;
120 return factory.actions().niciraSetNsp(niciraNshSpi.nshSpi().servicePathId());
121 }
Phaneendra Mandaab5a7362015-12-02 01:10:01 +0530122 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SI.type())) {
Phaneendra Manda0c0f14b2015-12-09 20:38:27 +0530123 NiciraSetNshSi niciraNshSi = (NiciraSetNshSi) extensionTreatment;
124 return factory.actions().niciraSetNsp(niciraNshSi.nshSi().serviceIndex());
Phaneendra Mandaab5a7362015-12-02 01:10:01 +0530125 }
126 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH1.type())) {
Phaneendra Manda0c0f14b2015-12-09 20:38:27 +0530127 NiciraSetNshContextHeader niciraNshch = (NiciraSetNshContextHeader) extensionTreatment;
128 return factory.actions().niciraSetNshc1(niciraNshch.nshCh().nshContextHeader());
Phaneendra Mandaab5a7362015-12-02 01:10:01 +0530129 }
130 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH2.type())) {
Phaneendra Manda0c0f14b2015-12-09 20:38:27 +0530131 NiciraSetNshContextHeader niciraNshch = (NiciraSetNshContextHeader) extensionTreatment;
132 return factory.actions().niciraSetNshc2(niciraNshch.nshCh().nshContextHeader());
Phaneendra Mandaab5a7362015-12-02 01:10:01 +0530133 }
134 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH3.type())) {
Phaneendra Manda0c0f14b2015-12-09 20:38:27 +0530135 NiciraSetNshContextHeader niciraNshch = (NiciraSetNshContextHeader) extensionTreatment;
136 return factory.actions().niciraSetNshc3(niciraNshch.nshCh().nshContextHeader());
Phaneendra Mandaab5a7362015-12-02 01:10:01 +0530137 }
138 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH4.type())) {
Phaneendra Manda0c0f14b2015-12-09 20:38:27 +0530139 NiciraSetNshContextHeader niciraNshch = (NiciraSetNshContextHeader) extensionTreatment;
140 return factory.actions().niciraSetNshc4(niciraNshch.nshCh().nshContextHeader());
Phaneendra Mandaab5a7362015-12-02 01:10:01 +0530141 }
samueljcc4f4d25a2015-12-07 10:52:28 +0800142 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SHA_TO_THA.type())
samueljcc4a527ed2015-12-03 13:59:49 +0800143 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SPA_TO_TPA.type())
144 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ETH_SRC_TO_DST.type())
145 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_IP_SRC_TO_DST.type())) {
samueljcc4f4d25a2015-12-07 10:52:28 +0800146 MoveExtensionTreatment mov = (MoveExtensionTreatment) extensionTreatment;
147 OFActionNiciraMove.Builder action = factory.actions()
148 .buildNiciraMove();
149 action.setDstOfs(mov.dstOffset());
150 action.setSrcOfs(mov.srcOffset());
151 action.setNBits(mov.nBits());
152 action.setSrc(mov.src());
153 action.setDst(mov.dst());
154 return action.build();
samueljcc4a527ed2015-12-03 13:59:49 +0800155 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700156 return null;
157 }
158
159 @Override
alshabib880b6442015-11-23 22:13:04 -0800160 public ExtensionTreatment mapAction(OFAction action) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700161 if (action.getType().equals(OFActionType.SET_FIELD)) {
162 OFActionSetField setFieldAction = (OFActionSetField) action;
163 OFOxm<?> oxm = setFieldAction.getField();
164 switch (oxm.getMatchField().id) {
165 case TUNNEL_IPV4_DST:
166 OFOxmTunnelIpv4Dst tunnelIpv4Dst = (OFOxmTunnelIpv4Dst) oxm;
167 return new NiciraSetTunnelDst(Ip4Address.valueOf(tunnelIpv4Dst.getValue().getInt()));
168 default:
169 throw new UnsupportedOperationException(
170 "Driver does not support extension type " + oxm.getMatchField().id);
171 }
172 }
samueljcc4f4d25a2015-12-07 10:52:28 +0800173 if (action.getType().equals(OFActionType.EXPERIMENTER)) {
174 OFActionExperimenter experimenter = (OFActionExperimenter) action;
175 if (Long.valueOf(experimenter.getExperimenter())
176 .intValue() == TYPE_NICIRA) {
177 OFActionNicira nicira = (OFActionNicira) experimenter;
178 if (nicira.getSubtype() == SUB_TYPE_MOVE) {
179 OFActionNiciraMove moveAction = (OFActionNiciraMove) nicira;
180 switch (Long.valueOf(moveAction.getSrc()).intValue()) {
181 case SRC_ARP_SHA:
182 return NiciraMoveTreatmentFactory
183 .createNiciraMovArpShaToTha();
184 case SRC_ETH:
185 return NiciraMoveTreatmentFactory
186 .createNiciraMovEthSrcToDst();
187 case SRC_IP:
188 return NiciraMoveTreatmentFactory
189 .createNiciraMovIpSrcToDst();
190 case SRC_ARP_SPA:
191 return NiciraMoveTreatmentFactory
192 .createNiciraMovArpSpaToTpa();
193 default:
194 throw new UnsupportedOperationException("Driver does not support move from "
195 + moveAction.getSrc() + " to "
196 + moveAction.getDst());
197 }
198 }
199 }
200 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700201 return null;
202 }
203
204 @Override
alshabib880b6442015-11-23 22:13:04 -0800205 public ExtensionTreatment getExtensionInstruction(ExtensionTreatmentType type) {
206 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type())) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700207 return new NiciraSetTunnelDst();
208 }
alshabib880b6442015-11-23 22:13:04 -0800209 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT.type())) {
BitOhenryb53ac6c2015-11-16 20:58:58 +0800210 return new NiciraResubmit();
211 }
Phaneendra Mandaab5a7362015-12-02 01:10:01 +0530212 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT_TABLE.type())) {
213 return new NiciraResubmitTable();
214 }
Phaneendra Mandace66cbc2015-11-26 18:07:48 +0530215 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SPI.type())) {
216 return new NiciraSetNshSpi();
217 }
Phaneendra Mandaab5a7362015-12-02 01:10:01 +0530218 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SI.type())) {
219 return new NiciraSetNshSi();
220 }
221 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH1.type())
222 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH2.type())
223 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH3.type())
224 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH4.type())) {
225 return new NiciraSetNshContextHeader(type);
BitOhenry74dd7e12015-12-01 09:07:19 +0800226 }
samueljcc4a527ed2015-12-03 13:59:49 +0800227 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SHA_TO_THA.type())) {
228 return NiciraMoveTreatmentFactory.createNiciraMovArpShaToTha();
229 }
230 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SPA_TO_TPA.type())) {
231 return NiciraMoveTreatmentFactory.createNiciraMovArpSpaToTpa();
232 }
233 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ETH_SRC_TO_DST.type())) {
234 return NiciraMoveTreatmentFactory.createNiciraMovEthSrcToDst();
235 }
236 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_IP_SRC_TO_DST.type())) {
237 return NiciraMoveTreatmentFactory.createNiciraMovIpSrcToDst();
238 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700239 throw new UnsupportedOperationException(
240 "Driver does not support extension type " + type.toString());
241 }
242}