Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [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.sfc.installer.impl; |
| 17 | |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SI; |
| 20 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SPI; |
| 21 | import static org.slf4j.LoggerFactory.getLogger; |
| 22 | |
| 23 | import java.util.LinkedList; |
| 24 | import java.util.List; |
| 25 | import java.util.ListIterator; |
| 26 | |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 27 | import org.onlab.osgi.DefaultServiceDirectory; |
| 28 | import org.onlab.osgi.ServiceDirectory; |
Mahesh Poojary S | 96cdcd2 | 2015-12-10 11:01:30 +0530 | [diff] [blame] | 29 | import org.onlab.packet.Ethernet; |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 30 | import org.onlab.packet.IPv4; |
| 31 | import org.onlab.packet.IpPrefix; |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 32 | import org.onlab.packet.MacAddress; |
| 33 | import org.onlab.packet.TpPort; |
| 34 | import org.onlab.packet.VlanId; |
| 35 | import org.onosproject.core.ApplicationId; |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 36 | import org.onosproject.net.ConnectPoint; |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 37 | import org.onosproject.net.DeviceId; |
Mahesh Poojary S | 96cdcd2 | 2015-12-10 11:01:30 +0530 | [diff] [blame] | 38 | import org.onosproject.net.Host; |
| 39 | import org.onosproject.net.HostId; |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 40 | import org.onosproject.net.NshServiceIndex; |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 41 | import org.onosproject.net.NshServicePathId; |
Mahesh Poojary S | 96cdcd2 | 2015-12-10 11:01:30 +0530 | [diff] [blame] | 42 | import org.onosproject.net.PortNumber; |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 43 | import org.onosproject.net.behaviour.ExtensionTreatmentResolver; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 44 | import org.onosproject.net.device.DeviceService; |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 45 | import org.onosproject.net.driver.DriverHandler; |
| 46 | import org.onosproject.net.driver.DriverService; |
| 47 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 48 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 49 | import org.onosproject.net.flow.TrafficSelector; |
| 50 | import org.onosproject.net.flow.TrafficTreatment; |
| 51 | import org.onosproject.net.flow.criteria.Criteria; |
| 52 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 53 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 54 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 55 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 56 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
| 57 | import org.onosproject.net.flowobjective.Objective; |
Mahesh Poojary S | 96cdcd2 | 2015-12-10 11:01:30 +0530 | [diff] [blame] | 58 | import org.onosproject.net.host.HostService; |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 59 | import org.onosproject.sfc.installer.FlowClassifierInstallerService; |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 60 | import org.onosproject.vtnrsc.FiveTuple; |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 61 | import org.onosproject.vtnrsc.FlowClassifier; |
| 62 | import org.onosproject.vtnrsc.FlowClassifierId; |
| 63 | import org.onosproject.vtnrsc.PortChain; |
| 64 | import org.onosproject.vtnrsc.PortPair; |
| 65 | import org.onosproject.vtnrsc.PortPairGroup; |
| 66 | import org.onosproject.vtnrsc.PortPairGroupId; |
| 67 | import org.onosproject.vtnrsc.PortPairId; |
| 68 | import org.onosproject.vtnrsc.VirtualPortId; |
| 69 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; |
| 70 | import org.onosproject.vtnrsc.portpair.PortPairService; |
| 71 | import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService; |
| 72 | import org.onosproject.vtnrsc.service.VtnRscService; |
| 73 | import org.onosproject.vtnrsc.virtualport.VirtualPortService; |
| 74 | import org.slf4j.Logger; |
| 75 | |
| 76 | /** |
| 77 | * Provides flow classifier installer implementation. |
| 78 | */ |
| 79 | public class FlowClassifierInstallerImpl implements FlowClassifierInstallerService { |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 80 | private final Logger log = getLogger(getClass()); |
| 81 | |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 82 | protected VirtualPortService virtualPortService; |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 83 | protected VtnRscService vtnRscService; |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 84 | protected PortPairService portPairService; |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 85 | protected PortPairGroupService portPairGroupService; |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 86 | protected FlowClassifierService flowClassifierService; |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 87 | protected DriverService driverService; |
Mahesh Poojary S | 96cdcd2 | 2015-12-10 11:01:30 +0530 | [diff] [blame] | 88 | protected DeviceService deviceService; |
Mahesh Poojary S | 96cdcd2 | 2015-12-10 11:01:30 +0530 | [diff] [blame] | 89 | protected HostService hostService; |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 90 | protected FlowObjectiveService flowObjectiveService; |
| 91 | protected ApplicationId appId; |
| 92 | |
| 93 | private static final String DRIVER_NAME = "onosfw"; |
| 94 | private static final String FLOW_CLASSIFIER_NOT_NULL = "Flow-Classifier cannot be null"; |
| 95 | private static final String FLOW_CLASSIFIER_ID_NOT_NULL = "Flow-Classifier-Id cannot be null"; |
| 96 | private static final String PORT_CHAIN_NOT_NULL = "Port-Chain cannot be null"; |
| 97 | private static final int NULL = 0; |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 98 | private static final int FLOW_CLASSIFIER_PRIORITY = 0x7fff; |
| 99 | private static final short NSH_SI_ID = 0xff; |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Default constructor. |
| 103 | */ |
| 104 | public FlowClassifierInstallerImpl() { |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Explicit constructor. |
| 109 | * |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 110 | * @param appId application id. |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 111 | */ |
| 112 | public FlowClassifierInstallerImpl(ApplicationId appId) { |
| 113 | this.appId = checkNotNull(appId, "ApplicationId can not be null"); |
| 114 | ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 115 | this.flowObjectiveService = serviceDirectory.get(FlowObjectiveService.class); |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 116 | this.driverService = serviceDirectory.get(DriverService.class); |
| 117 | this.deviceService = serviceDirectory.get(DeviceService.class); |
| 118 | this.hostService = serviceDirectory.get(HostService.class); |
| 119 | this.virtualPortService = serviceDirectory.get(VirtualPortService.class); |
| 120 | this.vtnRscService = serviceDirectory.get(VtnRscService.class); |
| 121 | this.portPairService = serviceDirectory.get(PortPairService.class); |
| 122 | this.portPairGroupService = serviceDirectory.get(PortPairGroupService.class); |
| 123 | this.flowClassifierService = serviceDirectory.get(FlowClassifierService.class); |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 127 | public ConnectPoint installFlowClassifier(PortChain portChain, NshServicePathId nshSpiId) { |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 128 | checkNotNull(portChain, PORT_CHAIN_NOT_NULL); |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 129 | // Get the portPairGroup |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 130 | List<PortPairGroupId> llPortPairGroupIdList = portChain.portPairGroups(); |
| 131 | ListIterator<PortPairGroupId> portPairGroupIdListIterator = llPortPairGroupIdList.listIterator(); |
| 132 | PortPairGroupId portPairGroupId = portPairGroupIdListIterator.next(); |
| 133 | PortPairGroup portPairGroup = portPairGroupService.getPortPairGroup(portPairGroupId); |
| 134 | List<PortPairId> llPortPairIdList = portPairGroup.portPairs(); |
| 135 | |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 136 | // Get port pair |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 137 | ListIterator<PortPairId> portPairListIterator = llPortPairIdList.listIterator(); |
| 138 | PortPairId portPairId = portPairListIterator.next(); |
| 139 | PortPair portPair = portPairService.getPortPair(portPairId); |
| 140 | |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 141 | return processFlowClassifier(portChain, portPair, nshSpiId, null, Objective.Operation.ADD); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public ConnectPoint unInstallFlowClassifier(PortChain portChain, NshServicePathId nshSpiId) { |
| 146 | checkNotNull(portChain, PORT_CHAIN_NOT_NULL); |
| 147 | // Get the portPairGroup |
| 148 | List<PortPairGroupId> llPortPairGroupIdList = portChain.portPairGroups(); |
| 149 | ListIterator<PortPairGroupId> portPairGroupIdListIterator = llPortPairGroupIdList.listIterator(); |
| 150 | PortPairGroupId portPairGroupId = portPairGroupIdListIterator.next(); |
| 151 | PortPairGroup portPairGroup = portPairGroupService.getPortPairGroup(portPairGroupId); |
| 152 | List<PortPairId> llPortPairIdList = portPairGroup.portPairs(); |
| 153 | |
| 154 | // Get port pair |
| 155 | ListIterator<PortPairId> portPairListIterator = llPortPairIdList.listIterator(); |
| 156 | PortPairId portPairId = portPairListIterator.next(); |
| 157 | PortPair portPair = portPairService.getPortPair(portPairId); |
| 158 | |
| 159 | return processFlowClassifier(portChain, portPair, nshSpiId, null, Objective.Operation.REMOVE); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public ConnectPoint installLoadBalancedFlowClassifier(PortChain portChain, FiveTuple fiveTuple, |
| 164 | NshServicePathId nshSpiId) { |
| 165 | checkNotNull(portChain, PORT_CHAIN_NOT_NULL); |
| 166 | |
| 167 | // Get the load balanced path |
| 168 | List<PortPairId> portPairs = portChain.getLoadBalancePath(fiveTuple); |
| 169 | |
| 170 | // Get the first port pair |
| 171 | ListIterator<PortPairId> portPairListIterator = portPairs.listIterator(); |
| 172 | PortPairId portPairId = portPairListIterator.next(); |
| 173 | PortPair portPair = portPairService.getPortPair(portPairId); |
| 174 | |
| 175 | return processFlowClassifier(portChain, portPair, nshSpiId, fiveTuple, Objective.Operation.ADD); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public ConnectPoint unInstallLoadBalancedFlowClassifier(PortChain portChain, FiveTuple fiveTuple, |
| 180 | NshServicePathId nshSpiId) { |
| 181 | checkNotNull(portChain, PORT_CHAIN_NOT_NULL); |
| 182 | // Get the load balanced path |
| 183 | List<PortPairId> portPairs = portChain.getLoadBalancePath(fiveTuple); |
| 184 | |
| 185 | // Get the first port pair |
| 186 | ListIterator<PortPairId> portPairListIterator = portPairs.listIterator(); |
| 187 | PortPairId portPairId = portPairListIterator.next(); |
| 188 | PortPair portPair = portPairService.getPortPair(portPairId); |
| 189 | |
| 190 | return processFlowClassifier(portChain, portPair, nshSpiId, fiveTuple, Objective.Operation.REMOVE); |
| 191 | } |
| 192 | |
| 193 | public ConnectPoint processFlowClassifier(PortChain portChain, PortPair portPair, NshServicePathId nshSpiId, |
| 194 | FiveTuple fiveTuple, Objective.Operation type) { |
| 195 | |
| 196 | DeviceId deviceIdfromPortPair = vtnRscService.getSfToSffMaping(VirtualPortId.portId(portPair.ingress())); |
| 197 | MacAddress srcMacAddress = virtualPortService.getPort(VirtualPortId.portId(portPair.ingress())).macAddress(); |
| 198 | Host host = hostService.getHost(HostId.hostId(srcMacAddress)); |
| 199 | PortNumber port = host.location().port(); |
| 200 | |
| 201 | DeviceId deviceId = deviceIdfromPortPair; |
| 202 | |
| 203 | // Vxlan tunnel port for NSH header(Vxlan + NSH). |
| 204 | TpPort nshDstPort = TpPort.tpPort(6633); |
| 205 | |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 206 | FlowClassifierInstallerService flowclassifierinstallerService; |
| 207 | // get flow classifiers |
| 208 | List<FlowClassifierId> llFlowClassifierList = portChain.flowClassifiers(); |
| 209 | ListIterator<FlowClassifierId> flowClassifierListIterator = llFlowClassifierList.listIterator(); |
| 210 | |
| 211 | while (flowClassifierListIterator.hasNext()) { |
| 212 | FlowClassifierId flowclassifierId = flowClassifierListIterator.next(); |
| 213 | FlowClassifier flowClassifier = flowClassifierService.getFlowClassifier(flowclassifierId); |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 214 | |
| 215 | if ((flowClassifier.srcPort() != null) && (!flowClassifier.srcPort().portId().isEmpty())) { |
| 216 | deviceId = vtnRscService.getSfToSffMaping(flowClassifier.srcPort()); |
| 217 | } |
| 218 | |
| 219 | // Build Traffic selector. |
| 220 | TrafficSelector.Builder selector = packTrafficSelector(flowClassifier, fiveTuple); |
| 221 | |
| 222 | if (fiveTuple == null) { |
| 223 | // Send the packet to controller |
| 224 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 225 | treatment.setOutput(PortNumber.CONTROLLER); |
| 226 | sendServiceFunctionClassifier(selector, treatment, deviceId, type); |
| 227 | } else if (deviceId.equals(deviceIdfromPortPair)) { |
| 228 | // classifier and source device are in the same OVS. So directly send packet to first port pair |
| 229 | TrafficTreatment.Builder treatment = packTrafficTreatment(deviceId, port, nshDstPort, |
| 230 | nshSpiId, flowClassifier, true); |
| 231 | // Build forwarding objective and send to OVS. |
| 232 | sendServiceFunctionClassifier(selector, treatment, deviceId, type); |
| 233 | } else { |
| 234 | // classifier and source device are not in the same OVS. Send packet on vlan Tunnel |
| 235 | TrafficTreatment.Builder treatment = packTrafficTreatment(deviceId, port, nshDstPort, |
| 236 | nshSpiId, flowClassifier, false); |
| 237 | // Build forwarding objective and send to OVS. |
| 238 | sendServiceFunctionClassifier(selector, treatment, deviceId, type); |
| 239 | |
| 240 | // At the other device get the packet from vlan and send to first port pair |
| 241 | TrafficSelector.Builder selectorDst = DefaultTrafficSelector.builder(); |
| 242 | selectorDst.matchVlanId((VlanId.vlanId(Short.parseShort((vtnRscService |
| 243 | .getL3vni(flowClassifier.tenantId()).toString()))))); |
| 244 | TrafficTreatment.Builder treatmentDst = DefaultTrafficTreatment.builder(); |
| 245 | Host hostDst = hostService.getHost(HostId.hostId(srcMacAddress)); |
| 246 | treatmentDst.setOutput(hostDst.location().port()); |
| 247 | sendServiceFunctionClassifier(selectorDst, treatmentDst, deviceIdfromPortPair, type); |
| 248 | } |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 249 | } |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 250 | return host.location(); |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Pack Traffic selector. |
| 255 | * |
| 256 | * @param flowClassifier flow-classifier |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 257 | * @param fiveTuple five tuple info for the packet |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 258 | * @return traffic selector |
| 259 | */ |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 260 | public TrafficSelector.Builder packTrafficSelector(FlowClassifier flowClassifier, FiveTuple fiveTuple) { |
| 261 | |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 262 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 263 | |
Mahesh Poojary S | 96cdcd2 | 2015-12-10 11:01:30 +0530 | [diff] [blame] | 264 | if ((flowClassifier.srcIpPrefix() != null) && (flowClassifier.srcIpPrefix().prefixLength() != 0)) { |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 265 | selector.matchIPSrc(flowClassifier.srcIpPrefix()); |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 266 | } else if (fiveTuple != null && fiveTuple.ipSrc() != null) { |
| 267 | selector.matchIPSrc(IpPrefix.valueOf(fiveTuple.ipSrc(), 24)); |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 268 | } |
Mahesh Poojary S | 96cdcd2 | 2015-12-10 11:01:30 +0530 | [diff] [blame] | 269 | |
| 270 | if ((flowClassifier.dstIpPrefix() != null) && (flowClassifier.dstIpPrefix().prefixLength() != 0)) { |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 271 | selector.matchIPDst(flowClassifier.dstIpPrefix()); |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 272 | } else if (fiveTuple != null && fiveTuple.ipDst() != null) { |
| 273 | selector.matchIPDst(IpPrefix.valueOf(fiveTuple.ipDst(), 24)); |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 274 | } |
| 275 | |
Mahesh Poojary S | 96cdcd2 | 2015-12-10 11:01:30 +0530 | [diff] [blame] | 276 | if ((flowClassifier.protocol() != null) && (!flowClassifier.protocol().isEmpty())) { |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 277 | if (flowClassifier.protocol().equalsIgnoreCase("TCP")) { |
| 278 | selector.add(Criteria.matchIPProtocol(IPv4.PROTOCOL_TCP)); |
| 279 | } else if (flowClassifier.protocol().equalsIgnoreCase("UDP")) { |
| 280 | selector.add(Criteria.matchIPProtocol(IPv4.PROTOCOL_UDP)); |
| 281 | } |
| 282 | } else if (fiveTuple != null && fiveTuple.protocol() != 0) { |
| 283 | selector.add(Criteria.matchIPProtocol(fiveTuple.protocol())); |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 284 | } |
Mahesh Poojary S | 96cdcd2 | 2015-12-10 11:01:30 +0530 | [diff] [blame] | 285 | |
| 286 | if (((flowClassifier.etherType() != null) && (!flowClassifier.etherType().isEmpty())) |
| 287 | && (flowClassifier.etherType().equals("IPv4") || flowClassifier.etherType().equals("IPv6"))) { |
| 288 | if (flowClassifier.etherType().equals("IPv4")) { |
| 289 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 290 | } else { |
| 291 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 292 | } |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 293 | } |
| 294 | |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 295 | if ((flowClassifier.srcPort() != null) && (!flowClassifier.srcPort().portId().isEmpty())) { |
| 296 | VirtualPortId vPortId = VirtualPortId.portId(flowClassifier.srcPort().portId()); |
| 297 | MacAddress macAddress = virtualPortService.getPort(vPortId).macAddress(); |
| 298 | Host host = hostService.getHost(HostId.hostId(macAddress)); |
| 299 | selector.matchInPort(host.location().port()); |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 300 | } |
| 301 | |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 302 | // Take the port information from five tuple only when the protocol is TCP. |
| 303 | if (fiveTuple != null && fiveTuple.protocol() == IPv4.PROTOCOL_TCP) { |
| 304 | selector.matchTcpSrc(TpPort.tpPort((int) fiveTuple.portSrc().toLong())); |
| 305 | selector.matchTcpDst(TpPort.tpPort((int) fiveTuple.portDst().toLong())); |
| 306 | } else { |
| 307 | // For udp packets take the port information from flow classifier |
| 308 | List<TpPort> srcPortRange = new LinkedList<>(); |
| 309 | List<TpPort> dstPortRange = new LinkedList<>(); |
| 310 | if ((flowClassifier.minSrcPortRange() != 0) && flowClassifier.maxSrcPortRange() != 0 |
| 311 | && flowClassifier.minDstPortRange() != 0 && flowClassifier.maxDstPortRange() != 0) { |
| 312 | |
| 313 | for (int port = flowClassifier.minSrcPortRange(); port <= flowClassifier.maxSrcPortRange(); port++) { |
| 314 | srcPortRange.add(TpPort.tpPort(port)); |
| 315 | } |
| 316 | for (int port = flowClassifier.minDstPortRange(); port <= flowClassifier.maxDstPortRange(); port++) { |
| 317 | dstPortRange.add(TpPort.tpPort(port)); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | for (TpPort inPort : srcPortRange) { |
| 322 | selector.matchUdpSrc(inPort); |
| 323 | } |
| 324 | for (TpPort outPort : dstPortRange) { |
| 325 | selector.matchUdpDst(outPort); |
| 326 | } |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 327 | } |
| 328 | return selector; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Pack traffic treatment. |
| 333 | * |
| 334 | * @param deviceId device id |
Phaneendra Manda | b8889b8 | 2016-03-04 14:13:10 +0530 | [diff] [blame] | 335 | * @param port port number |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 336 | * @param nshDstPort vxlan tunnel port for nsh header |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 337 | * @param nshSpi nsh spi |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 338 | * @param flowClassifier flow-classifier |
Phaneendra Manda | b8889b8 | 2016-03-04 14:13:10 +0530 | [diff] [blame] | 339 | * @param isSameOvs whether the next service function is in same ovs |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 340 | * @return traffic treatment |
| 341 | */ |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 342 | public TrafficTreatment.Builder packTrafficTreatment(DeviceId deviceId, PortNumber port, |
| 343 | TpPort nshDstPort, NshServicePathId nshSpi, |
| 344 | FlowClassifier flowClassifier, boolean isSameOvs) { |
| 345 | |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 346 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
Mahesh Poojary S | 96cdcd2 | 2015-12-10 11:01:30 +0530 | [diff] [blame] | 347 | |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 348 | if (isSameOvs) { |
Mahesh Poojary S | 96cdcd2 | 2015-12-10 11:01:30 +0530 | [diff] [blame] | 349 | treatmentBuilder.setOutput(port); |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 350 | } else { |
| 351 | treatmentBuilder.setVlanId((VlanId.vlanId(Short.parseShort((vtnRscService |
| 352 | .getL3vni(flowClassifier.tenantId()).toString()))))); |
| 353 | treatmentBuilder.setUdpDst(nshDstPort); |
Mahesh Poojary S | 96cdcd2 | 2015-12-10 11:01:30 +0530 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | // Set NSH |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 357 | DriverHandler handler = driverService.createHandler(deviceId); |
| 358 | ExtensionTreatmentResolver resolver = handler.behaviour(ExtensionTreatmentResolver.class); |
| 359 | ExtensionTreatment nspIdTreatment = resolver.getExtensionInstruction(NICIRA_SET_NSH_SPI.type()); |
| 360 | ExtensionTreatment nsiIdTreatment = resolver.getExtensionInstruction(NICIRA_SET_NSH_SI.type()); |
| 361 | |
| 362 | treatmentBuilder.extension(nspIdTreatment, deviceId); |
| 363 | treatmentBuilder.extension(nsiIdTreatment, deviceId); |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 364 | |
| 365 | try { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 366 | nspIdTreatment.setPropertyValue("nshSpi", nshSpi); |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 367 | } catch (Exception e) { |
| 368 | log.error("Failed to get extension instruction to set Nsh Spi Id {}", deviceId); |
| 369 | } |
| 370 | try { |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 371 | nsiIdTreatment.setPropertyValue("nshSi", NshServiceIndex.of(NSH_SI_ID)); |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 372 | } catch (Exception e) { |
| 373 | log.error("Failed to get extension instruction to set Nsh Si Id {}", deviceId); |
| 374 | } |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 375 | |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 376 | return treatmentBuilder; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Send service-function-forwarder to OVS. |
| 381 | * |
| 382 | * @param selector traffic selector |
| 383 | * @param treatment traffic treatment |
| 384 | * @param deviceId device id |
| 385 | * @param type operation type |
| 386 | */ |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 387 | public void sendServiceFunctionClassifier(TrafficSelector.Builder selector, TrafficTreatment.Builder treatment, |
| 388 | DeviceId deviceId, Objective.Operation type) { |
| 389 | log.info("Sending flow to service function classifier. Selector {}, Treatment {}", |
| 390 | selector.toString(), treatment.toString()); |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 391 | ForwardingObjective.Builder objective = DefaultForwardingObjective.builder().withTreatment(treatment.build()) |
Mahesh Poojary S | 96cdcd2 | 2015-12-10 11:01:30 +0530 | [diff] [blame] | 392 | .withSelector(selector.build()).fromApp(appId).makePermanent().withFlag(Flag.VERSATILE) |
Phaneendra Manda | 0f21ad6 | 2016-02-12 19:32:13 +0530 | [diff] [blame] | 393 | .withPriority(FLOW_CLASSIFIER_PRIORITY); |
Bharat Saraswal | 0fa12e9 | 2015-12-04 02:58:01 +0530 | [diff] [blame] | 394 | |
| 395 | if (type.equals(Objective.Operation.ADD)) { |
| 396 | log.debug("flowClassifierRules-->ADD"); |
| 397 | flowObjectiveService.forward(deviceId, objective.add()); |
| 398 | } else { |
| 399 | log.debug("flowClassifierRules-->REMOVE"); |
| 400 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 401 | } |
| 402 | } |
| 403 | } |