sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 1 | /* |
| 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 | package org.onosproject.provider.of.group.impl; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 19 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onlab.packet.MacAddress; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 22 | import org.onlab.packet.MplsLabel; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onosproject.core.DefaultGroupId; |
| 25 | import org.onosproject.core.GroupId; |
Sho SHIMIZU | 9553bb8 | 2015-06-30 16:02:45 -0700 | [diff] [blame^] | 26 | import org.onosproject.net.Lambda; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 29 | import org.onosproject.net.flow.TrafficTreatment; |
Sho SHIMIZU | 9553bb8 | 2015-06-30 16:02:45 -0700 | [diff] [blame^] | 30 | import org.onosproject.net.flow.instructions.Instructions; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 31 | import org.onosproject.net.group.DefaultGroupBucket; |
| 32 | import org.onosproject.net.group.GroupBucket; |
| 33 | import org.onosproject.net.group.GroupBuckets; |
| 34 | import org.projectfloodlight.openflow.protocol.OFBucket; |
| 35 | import org.projectfloodlight.openflow.protocol.OFGroupType; |
| 36 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 37 | import org.projectfloodlight.openflow.protocol.action.OFActionCircuit; |
| 38 | import org.projectfloodlight.openflow.protocol.action.OFActionCopyTtlIn; |
| 39 | import org.projectfloodlight.openflow.protocol.action.OFActionCopyTtlOut; |
| 40 | import org.projectfloodlight.openflow.protocol.action.OFActionDecMplsTtl; |
| 41 | import org.projectfloodlight.openflow.protocol.action.OFActionDecNwTtl; |
| 42 | import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter; |
Saurav Das | 8832990 | 2015-05-27 23:22:32 -0700 | [diff] [blame] | 43 | import org.projectfloodlight.openflow.protocol.action.OFActionGroup; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 44 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
| 45 | import org.projectfloodlight.openflow.protocol.action.OFActionPopMpls; |
| 46 | import org.projectfloodlight.openflow.protocol.action.OFActionPushMpls; |
| 47 | import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst; |
| 48 | import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc; |
| 49 | import org.projectfloodlight.openflow.protocol.action.OFActionSetField; |
| 50 | import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst; |
| 51 | import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc; |
| 52 | import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp; |
| 53 | import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid; |
| 54 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
| 55 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigidBasic; |
| 56 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 57 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
| 58 | import org.projectfloodlight.openflow.types.U32; |
| 59 | import org.projectfloodlight.openflow.types.VlanPcp; |
| 60 | import org.slf4j.Logger; |
| 61 | |
| 62 | import java.util.List; |
| 63 | |
| 64 | import static org.slf4j.LoggerFactory.getLogger; |
| 65 | |
| 66 | /* |
| 67 | * Builder for GroupBucketEntry. |
| 68 | */ |
| 69 | public class GroupBucketEntryBuilder { |
| 70 | |
| 71 | private List<OFBucket> ofBuckets; |
| 72 | private OFGroupType type; |
| 73 | |
| 74 | private final Logger log = getLogger(getClass()); |
| 75 | |
| 76 | /** |
| 77 | * Creates a builder. |
| 78 | * |
| 79 | * @param ofBuckets list of OFBucket |
| 80 | * @param type Group type |
| 81 | */ |
| 82 | public GroupBucketEntryBuilder(List<OFBucket> ofBuckets, OFGroupType type) { |
| 83 | this.ofBuckets = ofBuckets; |
| 84 | this.type = type; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Builds a GroupBuckets. |
| 89 | * |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 90 | * @return GroupBuckets object, a list of GroupBuckets |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 91 | */ |
| 92 | public GroupBuckets build() { |
| 93 | List<GroupBucket> bucketList = Lists.newArrayList(); |
| 94 | |
| 95 | for (OFBucket bucket: ofBuckets) { |
| 96 | TrafficTreatment treatment = buildTreatment(bucket.getActions()); |
| 97 | // TODO: Use GroupBucketEntry |
| 98 | GroupBucket groupBucket = null; |
| 99 | switch (type) { |
| 100 | case INDIRECT: |
| 101 | groupBucket = |
| 102 | DefaultGroupBucket.createIndirectGroupBucket(treatment); |
| 103 | break; |
| 104 | case SELECT: |
| 105 | groupBucket = |
| 106 | DefaultGroupBucket.createSelectGroupBucket(treatment); |
| 107 | break; |
| 108 | case FF: |
| 109 | PortNumber port = |
| 110 | PortNumber.portNumber(bucket.getWatchPort().getPortNumber()); |
| 111 | GroupId groupId = |
| 112 | new DefaultGroupId(bucket.getWatchGroup().getGroupNumber()); |
| 113 | groupBucket = |
| 114 | DefaultGroupBucket.createFailoverGroupBucket(treatment, |
| 115 | port, groupId); |
| 116 | break; |
| 117 | default: |
| 118 | log.error("Unsupported Group type : {}", type); |
| 119 | } |
| 120 | if (groupBucket != null) { |
| 121 | bucketList.add(groupBucket); |
| 122 | } |
| 123 | } |
| 124 | return new GroupBuckets(bucketList); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | private TrafficTreatment buildTreatment(List<OFAction> actions) { |
| 129 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
| 130 | // If this is a drop rule |
| 131 | if (actions.size() == 0) { |
| 132 | builder.drop(); |
| 133 | return builder.build(); |
| 134 | } |
| 135 | for (OFAction act : actions) { |
| 136 | switch (act.getType()) { |
| 137 | case OUTPUT: |
| 138 | OFActionOutput out = (OFActionOutput) act; |
| 139 | builder.setOutput( |
| 140 | PortNumber.portNumber(out.getPort().getPortNumber())); |
| 141 | break; |
| 142 | case SET_VLAN_VID: |
| 143 | OFActionSetVlanVid vlan = (OFActionSetVlanVid) act; |
| 144 | builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan())); |
| 145 | break; |
| 146 | case SET_VLAN_PCP: |
| 147 | OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act; |
| 148 | builder.setVlanPcp(pcp.getVlanPcp().getValue()); |
| 149 | break; |
Jonathan Hart | e106e4b | 2015-03-09 16:44:56 -0700 | [diff] [blame] | 150 | case POP_VLAN: |
| 151 | builder.popVlan(); |
| 152 | break; |
| 153 | case PUSH_VLAN: |
| 154 | builder.pushVlan(); |
| 155 | break; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 156 | case SET_DL_DST: |
| 157 | OFActionSetDlDst dldst = (OFActionSetDlDst) act; |
| 158 | builder.setEthDst( |
| 159 | MacAddress.valueOf(dldst.getDlAddr().getLong())); |
| 160 | break; |
| 161 | case SET_DL_SRC: |
| 162 | OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act; |
| 163 | builder.setEthSrc( |
| 164 | MacAddress.valueOf(dlsrc.getDlAddr().getLong())); |
| 165 | |
| 166 | break; |
| 167 | case SET_NW_DST: |
| 168 | OFActionSetNwDst nwdst = (OFActionSetNwDst) act; |
| 169 | IPv4Address di = nwdst.getNwAddr(); |
| 170 | builder.setIpDst(Ip4Address.valueOf(di.getInt())); |
| 171 | break; |
| 172 | case SET_NW_SRC: |
| 173 | OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act; |
| 174 | IPv4Address si = nwsrc.getNwAddr(); |
| 175 | builder.setIpSrc(Ip4Address.valueOf(si.getInt())); |
| 176 | break; |
| 177 | case EXPERIMENTER: |
| 178 | OFActionExperimenter exp = (OFActionExperimenter) act; |
| 179 | if (exp.getExperimenter() == 0x80005A06 || |
| 180 | exp.getExperimenter() == 0x748771) { |
| 181 | OFActionCircuit ct = (OFActionCircuit) exp; |
Sho SHIMIZU | 9553bb8 | 2015-06-30 16:02:45 -0700 | [diff] [blame^] | 182 | short lambda = ((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber(); |
| 183 | builder.add(Instructions.modL0Lambda(Lambda.indexedLambda(lambda))); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 184 | } else { |
| 185 | log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter()); |
| 186 | } |
| 187 | break; |
| 188 | case SET_FIELD: |
| 189 | OFActionSetField setField = (OFActionSetField) act; |
| 190 | handleSetField(builder, setField.getField()); |
| 191 | break; |
| 192 | case POP_MPLS: |
| 193 | OFActionPopMpls popMpls = (OFActionPopMpls) act; |
| 194 | builder.popMpls((short) popMpls.getEthertype().getValue()); |
| 195 | break; |
| 196 | case PUSH_MPLS: |
| 197 | OFActionPushMpls pushMpls = (OFActionPushMpls) act; |
| 198 | builder.pushMpls(); |
| 199 | break; |
| 200 | case COPY_TTL_IN: |
| 201 | OFActionCopyTtlIn copyTtlIn = (OFActionCopyTtlIn) act; |
| 202 | builder.copyTtlIn(); |
| 203 | break; |
| 204 | case COPY_TTL_OUT: |
| 205 | OFActionCopyTtlOut copyTtlOut = (OFActionCopyTtlOut) act; |
| 206 | builder.copyTtlOut(); |
| 207 | break; |
| 208 | case DEC_MPLS_TTL: |
| 209 | OFActionDecMplsTtl decMplsTtl = (OFActionDecMplsTtl) act; |
| 210 | builder.decMplsTtl(); |
| 211 | break; |
| 212 | case DEC_NW_TTL: |
| 213 | OFActionDecNwTtl decNwTtl = (OFActionDecNwTtl) act; |
| 214 | builder.decNwTtl(); |
| 215 | break; |
Saurav Das | 8832990 | 2015-05-27 23:22:32 -0700 | [diff] [blame] | 216 | case GROUP: |
| 217 | OFActionGroup grp = (OFActionGroup) act; |
| 218 | builder.group(new DefaultGroupId(grp.getGroup().getGroupNumber())); |
| 219 | break; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 220 | case SET_TP_DST: |
| 221 | case SET_TP_SRC: |
| 222 | case POP_PBB: |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 223 | case PUSH_PBB: |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 224 | case SET_MPLS_LABEL: |
| 225 | case SET_MPLS_TC: |
| 226 | case SET_MPLS_TTL: |
| 227 | case SET_NW_ECN: |
| 228 | case SET_NW_TOS: |
| 229 | case SET_NW_TTL: |
| 230 | case SET_QUEUE: |
| 231 | case STRIP_VLAN: |
| 232 | case ENQUEUE: |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 233 | default: |
| 234 | log.warn("Action type {} not yet implemented.", act.getType()); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return builder.build(); |
| 239 | } |
| 240 | |
| 241 | private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) { |
| 242 | switch (oxm.getMatchField().id) { |
| 243 | case VLAN_PCP: |
| 244 | @SuppressWarnings("unchecked") |
| 245 | OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm; |
| 246 | builder.setVlanPcp(vlanpcp.getValue().getValue()); |
| 247 | break; |
| 248 | case VLAN_VID: |
| 249 | @SuppressWarnings("unchecked") |
| 250 | OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm; |
| 251 | builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan())); |
| 252 | break; |
| 253 | case ETH_DST: |
| 254 | @SuppressWarnings("unchecked") |
| 255 | OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst = |
| 256 | (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm; |
| 257 | builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong())); |
| 258 | break; |
| 259 | case ETH_SRC: |
| 260 | @SuppressWarnings("unchecked") |
| 261 | OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc = |
| 262 | (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm; |
| 263 | builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong())); |
| 264 | break; |
| 265 | case IPV4_DST: |
| 266 | @SuppressWarnings("unchecked") |
| 267 | OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm; |
| 268 | builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt())); |
| 269 | break; |
| 270 | case IPV4_SRC: |
| 271 | @SuppressWarnings("unchecked") |
| 272 | OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm; |
| 273 | builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt())); |
| 274 | break; |
| 275 | case MPLS_LABEL: |
| 276 | @SuppressWarnings("unchecked") |
| 277 | OFOxm<U32> labelId = (OFOxm<U32>) oxm; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 278 | builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue())); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 279 | break; |
| 280 | case ARP_OP: |
| 281 | case ARP_SHA: |
| 282 | case ARP_SPA: |
| 283 | case ARP_THA: |
| 284 | case ARP_TPA: |
| 285 | case BSN_EGR_PORT_GROUP_ID: |
| 286 | case BSN_GLOBAL_VRF_ALLOWED: |
| 287 | case BSN_IN_PORTS_128: |
| 288 | case BSN_L3_DST_CLASS_ID: |
| 289 | case BSN_L3_INTERFACE_CLASS_ID: |
| 290 | case BSN_L3_SRC_CLASS_ID: |
| 291 | case BSN_LAG_ID: |
| 292 | case BSN_TCP_FLAGS: |
| 293 | case BSN_UDF0: |
| 294 | case BSN_UDF1: |
| 295 | case BSN_UDF2: |
| 296 | case BSN_UDF3: |
| 297 | case BSN_UDF4: |
| 298 | case BSN_UDF5: |
| 299 | case BSN_UDF6: |
| 300 | case BSN_UDF7: |
| 301 | case BSN_VLAN_XLATE_PORT_GROUP_ID: |
| 302 | case BSN_VRF: |
| 303 | case ETH_TYPE: |
| 304 | case ICMPV4_CODE: |
| 305 | case ICMPV4_TYPE: |
| 306 | case ICMPV6_CODE: |
| 307 | case ICMPV6_TYPE: |
| 308 | case IN_PHY_PORT: |
| 309 | case IN_PORT: |
| 310 | case IPV6_DST: |
| 311 | case IPV6_FLABEL: |
| 312 | case IPV6_ND_SLL: |
| 313 | case IPV6_ND_TARGET: |
| 314 | case IPV6_ND_TLL: |
| 315 | case IPV6_SRC: |
| 316 | case IP_DSCP: |
| 317 | case IP_ECN: |
| 318 | case IP_PROTO: |
| 319 | case METADATA: |
| 320 | case MPLS_TC: |
| 321 | case OCH_SIGID: |
| 322 | case OCH_SIGID_BASIC: |
| 323 | case OCH_SIGTYPE: |
| 324 | case OCH_SIGTYPE_BASIC: |
| 325 | case SCTP_DST: |
| 326 | case SCTP_SRC: |
| 327 | case TCP_DST: |
| 328 | case TCP_SRC: |
| 329 | case TUNNEL_ID: |
| 330 | case UDP_DST: |
| 331 | case UDP_SRC: |
| 332 | default: |
| 333 | log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id); |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | } |