Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +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.forwarder.impl; |
| 17 | |
Mahesh Poojary S | e5cbab9 | 2015-12-10 10:52:04 +0530 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Component; |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Reference; |
| 20 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Mahesh Poojary S | e5cbab9 | 2015-12-10 10:52:04 +0530 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Service; |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 22 | import org.onlab.osgi.DefaultServiceDirectory; |
| 23 | import org.onlab.osgi.ServiceDirectory; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onlab.packet.VlanId; |
| 26 | import org.onosproject.core.ApplicationId; |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
Mahesh Poojary S | e5cbab9 | 2015-12-10 10:52:04 +0530 | [diff] [blame] | 28 | import org.onosproject.net.Host; |
| 29 | import org.onosproject.net.HostId; |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 30 | import org.onosproject.net.NshServicePathId; |
Mahesh Poojary S | e5cbab9 | 2015-12-10 10:52:04 +0530 | [diff] [blame] | 31 | import org.onosproject.net.PortNumber; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 32 | import org.onosproject.net.behaviour.ExtensionSelectorResolver; |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 33 | import org.onosproject.net.driver.DriverHandler; |
| 34 | import org.onosproject.net.driver.DriverService; |
| 35 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 36 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 37 | import org.onosproject.net.flow.TrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 39 | import org.onosproject.net.flow.criteria.ExtensionSelector; |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 40 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 41 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 42 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 44 | import org.onosproject.net.flowobjective.Objective; |
Mahesh Poojary S | e5cbab9 | 2015-12-10 10:52:04 +0530 | [diff] [blame] | 45 | import org.onosproject.net.host.HostService; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 46 | import org.onosproject.sfc.forwarder.ServiceFunctionForwarderService; |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 47 | import org.onosproject.vtnrsc.PortChain; |
| 48 | import org.onosproject.vtnrsc.PortPair; |
| 49 | import org.onosproject.vtnrsc.PortPairGroup; |
| 50 | import org.onosproject.vtnrsc.PortPairGroupId; |
| 51 | import org.onosproject.vtnrsc.PortPairId; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 52 | import org.onosproject.vtnrsc.VirtualPortId; |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 53 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; |
| 54 | import org.onosproject.vtnrsc.portchain.PortChainService; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 55 | import org.onosproject.vtnrsc.portpair.PortPairService; |
| 56 | import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService; |
| 57 | import org.onosproject.vtnrsc.service.VtnRscService; |
| 58 | import org.onosproject.vtnrsc.virtualport.VirtualPortService; |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 59 | import org.slf4j.Logger; |
| 60 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 61 | import java.util.List; |
| 62 | import java.util.ListIterator; |
| 63 | |
| 64 | import static com.google.common.base.Preconditions.checkNotNull; |
| 65 | import static org.onosproject.net.flow.criteria.ExtensionSelectorType.ExtensionSelectorTypes.NICIRA_MATCH_NSH_SPI; |
| 66 | import static org.slf4j.LoggerFactory.getLogger; |
| 67 | |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 68 | /** |
| 69 | * Provides Service Function Forwarder implementation. |
| 70 | */ |
Mahesh Poojary S | e5cbab9 | 2015-12-10 10:52:04 +0530 | [diff] [blame] | 71 | @Component(immediate = true) |
| 72 | @Service |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 73 | public class ServiceFunctionForwarderImpl implements ServiceFunctionForwarderService { |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Mahesh Poojary S | e5cbab9 | 2015-12-10 10:52:04 +0530 | [diff] [blame] | 76 | protected HostService hostService; |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 78 | protected DriverService driverService; |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected VirtualPortService virtualPortService; |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected VtnRscService vtnRscService; |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected PortPairService portPairService; |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected PortPairGroupService portPairGroupService; |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected FlowClassifierService flowClassifierService; |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected PortChainService portChainService; |
| 91 | |
| 92 | private final Logger log = getLogger(getClass()); |
| 93 | protected ApplicationId appId; |
| 94 | protected FlowObjectiveService flowObjectiveService; |
| 95 | |
| 96 | private static final String DRIVER_NAME = "onosfw"; |
| 97 | private static final String PORT_CHAIN_NOT_NULL = "Port-Chain cannot be null"; |
| 98 | private static final String PORT_CHAIN_ID_NOT_NULL = "Port-Chain-Id cannot be null"; |
| 99 | private static final String APP_ID_NOT_NULL = "Application-Id cannot be null"; |
| 100 | private static final int NULL = 0; |
| 101 | |
| 102 | /** |
| 103 | * Default constructor. |
| 104 | */ |
| 105 | public ServiceFunctionForwarderImpl() { |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Explicit constructor. |
Bharat saraswal | 1adafb3 | 2015-12-11 01:29:44 +0530 | [diff] [blame] | 110 | * |
| 111 | * @param appId Application id |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 112 | */ |
| 113 | public ServiceFunctionForwarderImpl(ApplicationId appId) { |
| 114 | this.appId = checkNotNull(appId, APP_ID_NOT_NULL); |
| 115 | ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 116 | this.flowObjectiveService = serviceDirectory.get(FlowObjectiveService.class); |
| 117 | } |
| 118 | |
| 119 | @Override |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 120 | public void installForwardingRule(PortChain portChain, NshServicePathId nshSpi) { |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 121 | checkNotNull(portChain, PORT_CHAIN_NOT_NULL); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 122 | prepareServiceFunctionForwarder(portChain, nshSpi, Objective.Operation.ADD); |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 126 | public void unInstallForwardingRule(PortChain portChain, NshServicePathId nshSpi) { |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 127 | checkNotNull(portChain, PORT_CHAIN_NOT_NULL); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 128 | prepareServiceFunctionForwarder(portChain, nshSpi, Objective.Operation.REMOVE); |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 132 | public void prepareServiceFunctionForwarder(PortChain portChain, NshServicePathId nshSpi, |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 133 | Objective.Operation type) { |
| 134 | |
| 135 | // Go through the port pair group list |
| 136 | List<PortPairGroupId> portPairGrpList = portChain.portPairGroups(); |
| 137 | ListIterator<PortPairGroupId> listGrpIterator = portPairGrpList.listIterator(); |
| 138 | |
| 139 | // Get source port pair group |
| 140 | if (!listGrpIterator.hasNext()) { |
| 141 | return; |
| 142 | } |
| 143 | PortPairGroupId portPairGrpId = listGrpIterator.next(); |
| 144 | PortPairGroup currentPortPairGroup = portPairGroupService.getPortPairGroup(portPairGrpId); |
| 145 | |
| 146 | // Get destination port pair group |
| 147 | if (!listGrpIterator.hasNext()) { |
| 148 | return; |
| 149 | } |
| 150 | portPairGrpId = listGrpIterator.next(); |
| 151 | PortPairGroup nextPortPairGroup = portPairGroupService.getPortPairGroup(portPairGrpId); |
| 152 | |
| 153 | // push SFF to OVS |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 154 | pushServiceFunctionForwarder(currentPortPairGroup, nextPortPairGroup, listGrpIterator, nshSpi, type); |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Push service-function-forwarder to OVS. |
| 159 | * |
| 160 | * @param currentPortPairGroup current port-pair-group |
| 161 | * @param nextPortPairGroup next port-pair-group |
| 162 | * @param listGrpIterator pointer to port-pair-group list |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 163 | * @param nshSpi nsh service path id |
Bharat saraswal | 1adafb3 | 2015-12-11 01:29:44 +0530 | [diff] [blame] | 164 | * @param type objective type |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 165 | */ |
| 166 | public void pushServiceFunctionForwarder(PortPairGroup currentPortPairGroup, PortPairGroup nextPortPairGroup, |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 167 | ListIterator<PortPairGroupId> listGrpIterator, NshServicePathId nshSpi, Objective.Operation type) { |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 168 | DeviceId deviceId = null; |
| 169 | DeviceId currentDeviceId = null; |
| 170 | DeviceId nextDeviceId = null; |
| 171 | PortPairGroupId portPairGrpId = null; |
| 172 | |
| 173 | // Travel from SF to SF. |
| 174 | do { |
| 175 | // Get the required information on port pairs from source port pair |
| 176 | // group |
| 177 | List<PortPairId> portPairList = currentPortPairGroup.portPairs(); |
| 178 | ListIterator<PortPairId> portPLIterator = portPairList.listIterator(); |
| 179 | if (!portPLIterator.hasNext()) { |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | PortPairId portPairId = portPLIterator.next(); |
| 184 | PortPair portPair = portPairService.getPortPair(portPairId); |
| 185 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 186 | currentDeviceId = vtnRscService.getSfToSffMaping(VirtualPortId.portId(portPair.ingress())); |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 187 | if (deviceId == null) { |
| 188 | deviceId = currentDeviceId; |
| 189 | } |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 190 | |
| 191 | // pack traffic selector |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 192 | TrafficSelector.Builder selector = packTrafficSelector(deviceId, portPair, nshSpi); |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 193 | |
| 194 | // Get the required information on port pairs from destination port |
| 195 | // pair group |
| 196 | portPairList = nextPortPairGroup.portPairs(); |
| 197 | portPLIterator = portPairList.listIterator(); |
| 198 | if (!portPLIterator.hasNext()) { |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | portPairId = portPLIterator.next(); |
| 203 | portPair = portPairService.getPortPair(portPairId); |
| 204 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 205 | nextDeviceId = vtnRscService.getSfToSffMaping(VirtualPortId.portId(portPair.ingress())); |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 206 | |
| 207 | // pack traffic treatment |
| 208 | TrafficTreatment.Builder treatment = packTrafficTreatment(currentDeviceId, nextDeviceId, portPair); |
| 209 | |
| 210 | // Send SFF to OVS |
| 211 | sendServiceFunctionForwarder(selector, treatment, deviceId, type); |
| 212 | |
| 213 | // Replace source port pair group with destination port pair group |
| 214 | // for moving to next SFF processing. |
| 215 | currentPortPairGroup = nextPortPairGroup; |
| 216 | if (!listGrpIterator.hasNext()) { |
| 217 | break; |
| 218 | } |
| 219 | portPairGrpId = listGrpIterator.next(); |
| 220 | nextPortPairGroup = portPairGroupService.getPortPairGroup(portPairGrpId); |
| 221 | } while (true); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Pack Traffic selector. |
| 226 | * |
| 227 | * @param deviceId device id |
Mahesh Poojary S | e5cbab9 | 2015-12-10 10:52:04 +0530 | [diff] [blame] | 228 | * @param portPair port-pair |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 229 | * @param nshSpi nsh spi |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 230 | * @return traffic treatment |
| 231 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 232 | public TrafficSelector.Builder packTrafficSelector(DeviceId deviceId, PortPair portPair, NshServicePathId nshSpi) { |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 233 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
Mahesh Poojary S | e5cbab9 | 2015-12-10 10:52:04 +0530 | [diff] [blame] | 234 | MacAddress dstMacAddress = virtualPortService.getPort(VirtualPortId.portId(portPair.egress())).macAddress(); |
| 235 | Host host = hostService.getHost(HostId.hostId(dstMacAddress)); |
| 236 | PortNumber port = host.location().port(); |
| 237 | selector.matchInPort(port); |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 238 | |
| 239 | DriverHandler handler = driverService.createHandler(deviceId); |
| 240 | ExtensionSelectorResolver resolver = handler.behaviour(ExtensionSelectorResolver.class); |
| 241 | ExtensionSelector nspSpiSelector = resolver.getExtensionSelector(NICIRA_MATCH_NSH_SPI.type()); |
| 242 | |
| 243 | try { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 244 | nspSpiSelector.setPropertyValue("nshSpi", nshSpi); |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 245 | } catch (Exception e) { |
| 246 | log.error("Failed to get extension instruction to set Nsh Spi Id {}", deviceId); |
| 247 | } |
| 248 | |
| 249 | selector.extension(nspSpiSelector, deviceId); |
Mahesh Poojary S | e5cbab9 | 2015-12-10 10:52:04 +0530 | [diff] [blame] | 250 | |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 251 | return selector; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Pack Traffic treatment. |
| 256 | * |
| 257 | * @param currentDeviceId current device id |
| 258 | * @param nextDeviceId next device id |
| 259 | * @param portPair port-pair |
| 260 | * @return traffic treatment |
| 261 | */ |
| 262 | public TrafficTreatment.Builder packTrafficTreatment(DeviceId currentDeviceId, DeviceId nextDeviceId, |
| 263 | PortPair portPair) { |
| 264 | MacAddress srcMacAddress = null; |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 265 | |
| 266 | // Check the treatment whether destination SF is on same OVS or in |
| 267 | // different OVS. |
| 268 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 269 | if (currentDeviceId.equals(nextDeviceId)) { |
| 270 | srcMacAddress = virtualPortService.getPort(VirtualPortId.portId(portPair.ingress())).macAddress(); |
Mahesh Poojary S | e5cbab9 | 2015-12-10 10:52:04 +0530 | [diff] [blame] | 271 | |
| 272 | Host host = hostService.getHost(HostId.hostId(srcMacAddress)); |
| 273 | PortNumber port = host.location().port(); |
| 274 | treatment.setOutput(port); |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 275 | } else { |
Mahesh Poojary S | e5cbab9 | 2015-12-10 10:52:04 +0530 | [diff] [blame] | 276 | VlanId vlanId = VlanId.vlanId(Short.parseShort((vtnRscService.getL3vni(portPair.tenantId()).toString()))); |
| 277 | treatment.setVlanId(vlanId); |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | return treatment; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Send service function forwarder to OVS. |
| 285 | * |
| 286 | * @param selector traffic selector |
| 287 | * @param treatment traffic treatment |
| 288 | * @param deviceId device id |
| 289 | * @param type operation type |
| 290 | */ |
| 291 | public void sendServiceFunctionForwarder(TrafficSelector.Builder selector, TrafficTreatment.Builder treatment, |
| 292 | DeviceId deviceId, Objective.Operation type) { |
| 293 | ForwardingObjective.Builder objective = DefaultForwardingObjective.builder().withTreatment(treatment.build()) |
Mahesh Poojary S | e5cbab9 | 2015-12-10 10:52:04 +0530 | [diff] [blame] | 294 | .withSelector(selector.build()).fromApp(appId).makePermanent().withFlag(Flag.VERSATILE); |
Mahesh Poojary S | 7e9a78f | 2015-12-04 01:55:57 +0530 | [diff] [blame] | 295 | if (type.equals(Objective.Operation.ADD)) { |
| 296 | log.debug("ADD"); |
| 297 | flowObjectiveService.forward(deviceId, objective.add()); |
| 298 | } else { |
| 299 | log.debug("REMOVE"); |
| 300 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 301 | } |
| 302 | } |
| 303 | } |