Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 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.manager.impl; |
| 17 | |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 20 | import java.util.Collection; |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 21 | import java.util.Iterator; |
| 22 | import java.util.List; |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 23 | import java.util.ListIterator; |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 24 | import java.util.Set; |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 25 | import java.util.UUID; |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 26 | |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Activate; |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Deactivate; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 30 | import org.apache.felix.scr.annotations.Reference; |
| 31 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 32 | import org.apache.felix.scr.annotations.Service; |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 33 | import org.onlab.packet.Ethernet; |
| 34 | import org.onlab.packet.IPv4; |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 35 | import org.onlab.packet.IpAddress; |
| 36 | import org.onlab.packet.MacAddress; |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 37 | import org.onlab.packet.TCP; |
| 38 | import org.onlab.packet.UDP; |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 39 | import org.onlab.util.ItemNotFoundException; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 40 | import org.onlab.util.KryoNamespace; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 41 | import org.onosproject.core.ApplicationId; |
| 42 | import org.onosproject.core.CoreService; |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 43 | import org.onosproject.core.IdGenerator; |
| 44 | import org.onosproject.net.ConnectPoint; |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 45 | import org.onosproject.net.NshServicePathId; |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 46 | import org.onosproject.net.PortNumber; |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 47 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 48 | import org.onosproject.net.flow.TrafficTreatment; |
| 49 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 50 | import org.onosproject.net.packet.OutboundPacket; |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 51 | import org.onosproject.net.packet.PacketContext; |
| 52 | import org.onosproject.net.packet.PacketProcessor; |
| 53 | import org.onosproject.net.packet.PacketService; |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 54 | import org.onosproject.sfc.installer.impl.SfcFlowRuleInstallerImpl; |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 55 | import org.onosproject.sfc.manager.SfcService; |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 56 | import org.onosproject.store.serializers.KryoNamespaces; |
| 57 | import org.onosproject.store.service.DistributedSet; |
| 58 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 59 | import org.onosproject.store.service.Serializer; |
| 60 | import org.onosproject.store.service.StorageService; |
| 61 | import org.onosproject.store.service.WallClockTimestamp; |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 62 | import org.onosproject.vtnrsc.DefaultFiveTuple; |
| 63 | import org.onosproject.vtnrsc.FiveTuple; |
| 64 | import org.onosproject.vtnrsc.FixedIp; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 65 | import org.onosproject.vtnrsc.FlowClassifier; |
| 66 | import org.onosproject.vtnrsc.FlowClassifierId; |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 67 | import org.onosproject.vtnrsc.LoadBalanceId; |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 68 | import org.onosproject.vtnrsc.PortChain; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 69 | import org.onosproject.vtnrsc.PortChainId; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 70 | import org.onosproject.vtnrsc.PortPair; |
| 71 | import org.onosproject.vtnrsc.PortPairGroup; |
| 72 | import org.onosproject.vtnrsc.PortPairGroupId; |
| 73 | import org.onosproject.vtnrsc.PortPairId; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 74 | import org.onosproject.vtnrsc.TenantId; |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 75 | import org.onosproject.vtnrsc.VirtualPort; |
| 76 | import org.onosproject.vtnrsc.VirtualPortId; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 77 | import org.onosproject.vtnrsc.event.VtnRscEvent; |
| 78 | import org.onosproject.vtnrsc.event.VtnRscListener; |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 79 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; |
| 80 | import org.onosproject.vtnrsc.portchain.PortChainService; |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 81 | import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 82 | import org.onosproject.vtnrsc.service.VtnRscService; |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 83 | import org.onosproject.vtnrsc.virtualport.VirtualPortService; |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 84 | import org.slf4j.Logger; |
| 85 | |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 86 | import com.google.common.collect.Lists; |
| 87 | |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 88 | /** |
| 89 | * Provides implementation of SFC Service. |
| 90 | */ |
| 91 | @Component(immediate = true) |
| 92 | @Service |
| 93 | public class SfcManager implements SfcService { |
| 94 | |
| 95 | private final Logger log = getLogger(getClass()); |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 96 | |
| 97 | private String nshSpiIdTopic = "nsh-spi-id"; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 98 | private static final String APP_ID = "org.onosproject.app.vtn"; |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 99 | private static final int SFC_PRIORITY = 1000; |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 100 | private static final int MAX_NSH_SPI_ID = 0x7FFFF; |
| 101 | private static final int MAX_LOAD_BALANCE_ID = 0x20; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected VtnRscService vtnRscService; |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected CoreService coreService; |
| 108 | |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected PacketService packetService; |
| 111 | |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 113 | protected PortChainService portChainService; |
| 114 | |
| 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 116 | protected PortPairGroupService portPairGroupService; |
| 117 | |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 119 | protected FlowClassifierService flowClassifierService; |
| 120 | |
| 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 122 | protected VirtualPortService virtualPortService; |
| 123 | |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 125 | protected StorageService storageService; |
| 126 | |
| 127 | protected SfcPacketProcessor processor = new SfcPacketProcessor(); |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 128 | |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 129 | protected ApplicationId appId; |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 130 | protected IdGenerator nshSpiIdGenerator; |
| 131 | protected EventuallyConsistentMap<PortChainId, Integer> nshSpiPortChainMap; |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 132 | protected EventuallyConsistentMap<PortChainId, List<FiveTuple>> portChainFiveTupleMap; |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 133 | protected DistributedSet<Integer> nshSpiIdFreeList; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 134 | |
| 135 | private final VtnRscListener vtnRscListener = new InnerVtnRscListener(); |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 136 | |
| 137 | @Activate |
| 138 | public void activate() { |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 139 | appId = coreService.registerApplication(APP_ID); |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 140 | nshSpiIdGenerator = coreService.getIdGenerator(nshSpiIdTopic); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 141 | |
| 142 | vtnRscService.addListener(vtnRscListener); |
| 143 | |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 144 | KryoNamespace.Builder serializer = KryoNamespace |
| 145 | .newBuilder() |
| 146 | .register(PortChainId.class, UUID.class, FiveTuple.class, IpAddress.class, PortNumber.class, |
| 147 | DefaultFiveTuple.class, IpAddress.Version.class, TenantId.class); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 148 | |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 149 | nshSpiPortChainMap = storageService.<PortChainId, Integer>eventuallyConsistentMapBuilder() |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 150 | .withName("nshSpiPortChainMap").withSerializer(serializer) |
| 151 | .withTimestampProvider((k, v) ->new WallClockTimestamp()).build(); |
| 152 | |
| 153 | portChainFiveTupleMap = storageService.<PortChainId, List<FiveTuple>>eventuallyConsistentMapBuilder() |
| 154 | .withName("portChainFiveTupleMap").withSerializer(serializer) |
| 155 | .withTimestampProvider((k, v) ->new WallClockTimestamp()).build(); |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 156 | |
| 157 | nshSpiIdFreeList = storageService.<Integer>setBuilder() |
| 158 | .withName("nshSpiIdDeletedList") |
| 159 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 160 | .build() |
| 161 | .asDistributedSet(); |
| 162 | |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 163 | packetService.addProcessor(processor, PacketProcessor.director(SFC_PRIORITY)); |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 164 | log.info("Started"); |
| 165 | } |
| 166 | |
| 167 | @Deactivate |
| 168 | public void deactivate() { |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 169 | vtnRscService.removeListener(vtnRscListener); |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 170 | packetService.removeProcessor(processor); |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 171 | log.info("Stopped"); |
| 172 | } |
| 173 | |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 174 | /* |
| 175 | * Handle events. |
| 176 | */ |
| 177 | private class InnerVtnRscListener implements VtnRscListener { |
| 178 | @Override |
| 179 | public void event(VtnRscEvent event) { |
| 180 | |
| 181 | if (VtnRscEvent.Type.PORT_PAIR_PUT == event.type()) { |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 182 | PortPair portPair = event.subject().portPair(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 183 | onPortPairCreated(portPair); |
| 184 | } else if (VtnRscEvent.Type.PORT_PAIR_DELETE == event.type()) { |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 185 | PortPair portPair = event.subject().portPair(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 186 | onPortPairDeleted(portPair); |
| 187 | } else if (VtnRscEvent.Type.PORT_PAIR_UPDATE == event.type()) { |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 188 | PortPair portPair = event.subject().portPair(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 189 | onPortPairDeleted(portPair); |
| 190 | onPortPairCreated(portPair); |
| 191 | } else if (VtnRscEvent.Type.PORT_PAIR_GROUP_PUT == event.type()) { |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 192 | PortPairGroup portPairGroup = event.subject().portPairGroup(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 193 | onPortPairGroupCreated(portPairGroup); |
| 194 | } else if (VtnRscEvent.Type.PORT_PAIR_GROUP_DELETE == event.type()) { |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 195 | PortPairGroup portPairGroup = event.subject().portPairGroup(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 196 | onPortPairGroupDeleted(portPairGroup); |
| 197 | } else if (VtnRscEvent.Type.PORT_PAIR_GROUP_UPDATE == event.type()) { |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 198 | PortPairGroup portPairGroup = event.subject().portPairGroup(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 199 | onPortPairGroupDeleted(portPairGroup); |
| 200 | onPortPairGroupCreated(portPairGroup); |
| 201 | } else if (VtnRscEvent.Type.FLOW_CLASSIFIER_PUT == event.type()) { |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 202 | FlowClassifier flowClassifier = event.subject().flowClassifier(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 203 | onFlowClassifierCreated(flowClassifier); |
| 204 | } else if (VtnRscEvent.Type.FLOW_CLASSIFIER_DELETE == event.type()) { |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 205 | FlowClassifier flowClassifier = event.subject().flowClassifier(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 206 | onFlowClassifierDeleted(flowClassifier); |
| 207 | } else if (VtnRscEvent.Type.FLOW_CLASSIFIER_UPDATE == event.type()) { |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 208 | FlowClassifier flowClassifier = event.subject().flowClassifier(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 209 | onFlowClassifierDeleted(flowClassifier); |
| 210 | onFlowClassifierCreated(flowClassifier); |
| 211 | } else if (VtnRscEvent.Type.PORT_CHAIN_PUT == event.type()) { |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 212 | PortChain portChain = event.subject().portChain(); |
| 213 | if (portChain.oldPortChain() != null) { |
| 214 | onPortChainDeleted(portChain.oldPortChain()); |
| 215 | } |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 216 | onPortChainCreated(portChain); |
| 217 | } else if (VtnRscEvent.Type.PORT_CHAIN_DELETE == event.type()) { |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 218 | PortChain portChain = event.subject().portChain(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 219 | onPortChainDeleted(portChain); |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 220 | portChainFiveTupleMap.remove(portChain.portChainId()); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 221 | } else if (VtnRscEvent.Type.PORT_CHAIN_UPDATE == event.type()) { |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 222 | PortChain portChain = event.subject().portChain(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 223 | onPortChainDeleted(portChain); |
| 224 | onPortChainCreated(portChain); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 229 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 230 | public void onPortPairCreated(PortPair portPair) { |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 231 | log.debug("onPortPairCreated"); |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 232 | // Do nothing |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 236 | public void onPortPairDeleted(PortPair portPair) { |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 237 | log.debug("onPortPairDeleted"); |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 238 | // Do nothing |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 242 | public void onPortPairGroupCreated(PortPairGroup portPairGroup) { |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 243 | log.debug("onPortPairGroupCreated"); |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 244 | // Do nothing |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 248 | public void onPortPairGroupDeleted(PortPairGroup portPairGroup) { |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 249 | log.debug("onPortPairGroupDeleted"); |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 250 | // Do nothing |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 254 | public void onFlowClassifierCreated(FlowClassifier flowClassifier) { |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 255 | log.debug("onFlowClassifierCreated"); |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 256 | // Do nothing |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 260 | public void onFlowClassifierDeleted(FlowClassifier flowClassifier) { |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 261 | log.debug("onFlowClassifierDeleted"); |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 262 | // Do nothing |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 266 | public void onPortChainCreated(PortChain portChain) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 267 | NshServicePathId nshSpi; |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 268 | log.info("On port chain created"); |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 269 | |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 270 | int spi = getNextNshSpi(); |
| 271 | if (spi > MAX_NSH_SPI_ID) { |
| 272 | log.error("Reached max limit of service path index." + "Failed to install SFC for port chain {}", |
| 273 | portChain.portChainId().toString()); |
| 274 | return; |
| 275 | } |
| 276 | nshSpi = NshServicePathId.of(spi); |
| 277 | nshSpiPortChainMap.put(portChain.portChainId(), new Integer(spi)); |
| 278 | if (!portChainFiveTupleMap.containsKey(portChain.portChainId())) { |
| 279 | portChainFiveTupleMap.put(portChain.portChainId(), Lists.newArrayList()); |
| 280 | } |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 281 | // Install classifier rule to send the packet to controller |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 282 | SfcFlowRuleInstallerImpl flowRuleInstaller = new SfcFlowRuleInstallerImpl(appId); |
| 283 | flowRuleInstaller.installFlowClassifier(portChain, nshSpi); |
| 284 | |
| 285 | // Install rules for already identified five tuples. |
| 286 | List<FiveTuple> list = portChainFiveTupleMap.get(portChain.portChainId()); |
| 287 | for (FiveTuple fiveTuple : list) { |
| 288 | LoadBalanceId id = loadBalanceSfc(portChain.portChainId(), fiveTuple); |
| 289 | // Get nsh service path index |
| 290 | nshSpi = NshServicePathId.of(getNshServicePathId(id, spi)); |
| 291 | // download the required flow rules for classifier and |
| 292 | // forwarding |
| 293 | flowRuleInstaller.installLoadBalancedFlowRules(portChain, fiveTuple, nshSpi); |
| 294 | } |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 298 | public void onPortChainDeleted(PortChain portChain) { |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 299 | log.info("onPortChainDeleted"); |
| 300 | if (!nshSpiPortChainMap.containsKey(portChain.portChainId())) { |
| 301 | throw new ItemNotFoundException("Unable to find NSH SPI"); |
| 302 | } |
| 303 | |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 304 | int nshSpiId = nshSpiPortChainMap.get(portChain.portChainId()); |
| 305 | // Uninstall classifier rules |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 306 | SfcFlowRuleInstallerImpl flowRuleInstaller = new SfcFlowRuleInstallerImpl(appId); |
| 307 | flowRuleInstaller.unInstallFlowClassifier(portChain, NshServicePathId.of(nshSpiId)); |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 308 | // remove from nshSpiPortChainMap and add to nshSpiIdFreeList |
| 309 | nshSpiPortChainMap.remove(portChain.portChainId()); |
| 310 | nshSpiIdFreeList.add(nshSpiId); |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 311 | |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 312 | // Uninstall load balanced classifier and forwarding rules. |
| 313 | NshServicePathId nshSpi; |
| 314 | LoadBalanceId id; |
| 315 | List<LoadBalanceId> processedIdList = Lists.newArrayList(); |
| 316 | Set<FiveTuple> fiveTupleSet = portChain.getLoadBalanceIdMapKeys(); |
| 317 | for (FiveTuple fiveTuple : fiveTupleSet) { |
| 318 | id = portChain.getLoadBalanceId(fiveTuple); |
| 319 | if (processedIdList.contains(id)) { |
| 320 | // multiple five tuple can have single path. |
| 321 | continue; |
| 322 | } else { |
| 323 | processedIdList.add(id); |
| 324 | } |
| 325 | nshSpi = NshServicePathId.of(getNshServicePathId(id, nshSpiId)); |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 326 | flowRuleInstaller.unInstallLoadBalancedFlowRules(portChain, fiveTuple, nshSpi); |
| 327 | } |
| 328 | |
| 329 | // Reset load for all the port pairs |
| 330 | List<PortPairGroupId> ppgIdlist = portChain.portPairGroups(); |
| 331 | ListIterator<PortPairGroupId> ppgIdListIterator = ppgIdlist.listIterator(); |
| 332 | while (ppgIdListIterator.hasNext()) { |
| 333 | PortPairGroupId portPairGroupId = ppgIdListIterator.next(); |
| 334 | PortPairGroup ppg = portPairGroupService.getPortPairGroup(portPairGroupId); |
| 335 | ppg.resetLoad(); |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Get next nsh service path identifier. |
| 341 | * |
| 342 | * @return value of service path identifier |
| 343 | */ |
| 344 | int getNextNshSpi() { |
| 345 | // If there is any free id use it. Otherwise generate new id. |
| 346 | if (nshSpiIdFreeList.isEmpty()) { |
| 347 | return (int) nshSpiIdGenerator.getNewId(); |
| 348 | } |
| 349 | Iterator<Integer> it = nshSpiIdFreeList.iterator(); |
| 350 | Integer value = it.next(); |
| 351 | nshSpiIdFreeList.remove(value); |
| 352 | return value; |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 353 | } |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 354 | |
| 355 | private class SfcPacketProcessor implements PacketProcessor { |
| 356 | |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 357 | /** |
| 358 | * Check for given ip match with the fixed ips for the virtual port. |
| 359 | * |
| 360 | * @param vPortId virtual port id |
| 361 | * @param ip ip address to match |
| 362 | * @return true if the ip match with the fixed ips in virtual port false otherwise |
| 363 | */ |
| 364 | private boolean checkIpInVirtualPort(VirtualPortId vPortId, IpAddress ip) { |
| 365 | boolean found = false; |
| 366 | Set<FixedIp> ips = virtualPortService.getPort(vPortId).fixedIps(); |
| 367 | for (FixedIp fixedIp : ips) { |
| 368 | if (fixedIp.ip().equals(ip)) { |
| 369 | found = true; |
| 370 | break; |
| 371 | } |
| 372 | } |
| 373 | return found; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Find the port chain for the received packet. |
| 378 | * |
| 379 | * @param fiveTuple five tuple info from the packet |
| 380 | * @return portChainId id of port chain |
| 381 | */ |
| 382 | private PortChainId findPortChainFromFiveTuple(FiveTuple fiveTuple) { |
| 383 | |
| 384 | PortChainId portChainId = null; |
| 385 | |
| 386 | Iterable<PortChain> portChains = portChainService.getPortChains(); |
| 387 | if (portChains == null) { |
| 388 | log.error("Could not retrive port chain list"); |
| 389 | } |
| 390 | |
| 391 | // Identify the port chain to which the packet belongs |
| 392 | for (final PortChain portChain : portChains) { |
| 393 | |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 394 | if (!portChain.tenantId().equals(fiveTuple.tenantId())) { |
| 395 | continue; |
| 396 | } |
| 397 | |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 398 | Iterable<FlowClassifierId> flowClassifiers = portChain.flowClassifiers(); |
| 399 | |
| 400 | // One port chain can have multiple flow classifiers. |
| 401 | for (final FlowClassifierId flowClassifierId : flowClassifiers) { |
| 402 | |
| 403 | FlowClassifier flowClassifier = flowClassifierService.getFlowClassifier(flowClassifierId); |
| 404 | boolean match = false; |
| 405 | // Check whether protocol is set in flow classifier |
| 406 | if (flowClassifier.protocol() != null) { |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 407 | if ((flowClassifier.protocol().equalsIgnoreCase("TCP") |
| 408 | && fiveTuple.protocol() == IPv4.PROTOCOL_TCP) |
| 409 | || (flowClassifier.protocol().equalsIgnoreCase("UDP") |
| 410 | && fiveTuple.protocol() == IPv4.PROTOCOL_UDP) |
| 411 | || (flowClassifier.protocol().equalsIgnoreCase("ICMP") |
| 412 | && fiveTuple.protocol() == IPv4.PROTOCOL_ICMP)) { |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 413 | match = true; |
| 414 | } else { |
| 415 | continue; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // Check whether source ip prefix is set in flow classifier |
| 420 | if (flowClassifier.srcIpPrefix() != null) { |
| 421 | if (flowClassifier.srcIpPrefix().contains(fiveTuple.ipSrc())) { |
| 422 | match = true; |
| 423 | } else { |
| 424 | continue; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // Check whether destination ip prefix is set in flow classifier |
| 429 | if (flowClassifier.dstIpPrefix() != null) { |
| 430 | if (flowClassifier.dstIpPrefix().contains(fiveTuple.ipDst())) { |
| 431 | match = true; |
| 432 | } else { |
| 433 | continue; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | // Check whether source port is set in flow classifier |
| 438 | if (fiveTuple.portSrc().toLong() >= flowClassifier.minSrcPortRange() || |
| 439 | fiveTuple.portSrc().toLong() <= flowClassifier.maxSrcPortRange()) { |
| 440 | match = true; |
| 441 | } else { |
| 442 | continue; |
| 443 | } |
| 444 | |
| 445 | // Check whether destination port is set in flow classifier |
| 446 | if (fiveTuple.portDst().toLong() >= flowClassifier.minSrcPortRange() || |
| 447 | fiveTuple.portDst().toLong() <= flowClassifier.maxSrcPortRange()) { |
| 448 | match = true; |
| 449 | } else { |
| 450 | continue; |
| 451 | } |
| 452 | |
| 453 | // Check whether neutron source port is set in flow classfier |
| 454 | if ((flowClassifier.srcPort() != null) && (!flowClassifier.srcPort().portId().isEmpty())) { |
| 455 | match = checkIpInVirtualPort(VirtualPortId.portId(flowClassifier.srcPort().portId()), |
| 456 | fiveTuple.ipSrc()); |
| 457 | if (!match) { |
| 458 | continue; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // Check whether destination neutron destination port is set in flow classifier |
| 463 | if ((flowClassifier.dstPort() != null) && (!flowClassifier.dstPort().portId().isEmpty())) { |
| 464 | match = checkIpInVirtualPort(VirtualPortId.portId(flowClassifier.dstPort().portId()), |
| 465 | fiveTuple.ipDst()); |
| 466 | if (!match) { |
| 467 | continue; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | if (match) { |
| 472 | portChainId = portChain.portChainId(); |
| 473 | break; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | return portChainId; |
| 478 | } |
| 479 | |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 480 | /** |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 481 | * Get the tenant id for the given mac address. |
| 482 | * |
| 483 | * @param mac mac address |
| 484 | * @return tenantId tenant id for the given mac address |
| 485 | */ |
| 486 | private TenantId getTenantId(MacAddress mac) { |
| 487 | Collection<VirtualPort> virtualPorts = virtualPortService.getPorts(); |
| 488 | for (VirtualPort virtualPort : virtualPorts) { |
| 489 | if (virtualPort.macAddress().equals(mac)) { |
| 490 | return virtualPort.tenantId(); |
| 491 | } |
| 492 | } |
| 493 | return null; |
| 494 | } |
| 495 | |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 496 | @Override |
| 497 | public void process(PacketContext context) { |
| 498 | Ethernet packet = context.inPacket().parsed(); |
Phaneendra Manda | 089274b | 2016-04-11 17:21:56 +0530 | [diff] [blame] | 499 | if (packet == null || portChainService.getPortChainCount() == 0) { |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 500 | return; |
| 501 | } |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 502 | // get the five tuple parameters for the packet |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 503 | short ethType = packet.getEtherType(); |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 504 | IpAddress ipSrc = null; |
| 505 | IpAddress ipDst = null; |
| 506 | int portSrc = 0; |
| 507 | int portDst = 0; |
| 508 | byte protocol = 0; |
| 509 | MacAddress macSrc = packet.getSourceMAC(); |
Phaneendra Manda | 2360d25 | 2016-06-15 18:53:30 +0530 | [diff] [blame^] | 510 | MacAddress macDst = packet.getDestinationMAC(); |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 511 | TenantId tenantId = getTenantId(macSrc); |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 512 | |
| 513 | if (ethType == Ethernet.TYPE_IPV4) { |
| 514 | IPv4 ipv4Packet = (IPv4) packet.getPayload(); |
| 515 | ipSrc = IpAddress.valueOf(ipv4Packet.getSourceAddress()); |
| 516 | ipDst = IpAddress.valueOf(ipv4Packet.getDestinationAddress()); |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 517 | protocol = ipv4Packet.getProtocol(); |
| 518 | if (protocol == IPv4.PROTOCOL_TCP) { |
| 519 | TCP tcpPacket = (TCP) ipv4Packet.getPayload(); |
| 520 | portSrc = tcpPacket.getSourcePort(); |
| 521 | portDst = tcpPacket.getDestinationPort(); |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 522 | } else if (protocol == IPv4.PROTOCOL_UDP) { |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 523 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 524 | portSrc = udpPacket.getSourcePort(); |
| 525 | portDst = udpPacket.getDestinationPort(); |
Phaneendra Manda | 089274b | 2016-04-11 17:21:56 +0530 | [diff] [blame] | 526 | } else if (protocol == IPv4.PROTOCOL_ICMP) { |
| 527 | // do nothing |
| 528 | } else { |
| 529 | // No need to process other packets received by controller. |
| 530 | return; |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 531 | } |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 532 | } else { |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 533 | return; |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 534 | } |
| 535 | |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 536 | FiveTuple fiveTuple = DefaultFiveTuple.builder() |
| 537 | .setIpSrc(ipSrc) |
| 538 | .setIpDst(ipDst) |
| 539 | .setPortSrc(PortNumber.portNumber(portSrc)) |
| 540 | .setPortDst(PortNumber.portNumber(portDst)) |
| 541 | .setProtocol(protocol) |
| 542 | .setTenantId(tenantId) |
Phaneendra Manda | 2360d25 | 2016-06-15 18:53:30 +0530 | [diff] [blame^] | 543 | .setMacSrc(macSrc) |
| 544 | .setMacDst(macDst) |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 545 | .build(); |
| 546 | |
| 547 | PortChainId portChainId = findPortChainFromFiveTuple(fiveTuple); |
| 548 | |
| 549 | if (portChainId == null) { |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 550 | return; |
| 551 | } |
| 552 | |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 553 | // Once the 5 tuple and port chain are identified, give this input for load balancing |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 554 | addToPortChainIdFiveTupleMap(portChainId, fiveTuple); |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 555 | LoadBalanceId id = loadBalanceSfc(portChainId, fiveTuple); |
| 556 | // Get nsh service path index |
| 557 | NshServicePathId nshSpi; |
| 558 | PortChain portChain = portChainService.getPortChain(portChainId); |
| 559 | if (nshSpiPortChainMap.containsKey(portChain.portChainId())) { |
| 560 | int nshSpiId = nshSpiPortChainMap.get(portChain.portChainId()); |
| 561 | nshSpi = NshServicePathId.of(getNshServicePathId(id, nshSpiId)); |
| 562 | } else { |
| 563 | int nshSpiId = getNextNshSpi(); |
| 564 | if (nshSpiId > MAX_NSH_SPI_ID) { |
| 565 | log.error("Reached max limit of service path index." |
| 566 | + "Failed to install SFC for port chain {}", portChain.portChainId()); |
| 567 | return; |
| 568 | } |
| 569 | nshSpi = NshServicePathId.of(getNshServicePathId(id, nshSpiId)); |
| 570 | nshSpiPortChainMap.put(portChain.portChainId(), new Integer(nshSpiId)); |
| 571 | } |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 572 | // download the required flow rules for classifier and forwarding |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 573 | // install in OVS. |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 574 | SfcFlowRuleInstallerImpl flowRuleInstaller = new SfcFlowRuleInstallerImpl(appId); |
| 575 | flowRuleInstaller.installLoadBalancedFlowRules(portChain, fiveTuple, nshSpi); |
| 576 | sendPacket(context); |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 577 | } |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 578 | |
| 579 | /** |
| 580 | * Send packet back to classifier. |
| 581 | * |
| 582 | * @param context packet context |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 583 | */ |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 584 | private void sendPacket(PacketContext context) { |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 585 | |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 586 | ConnectPoint sourcePoint = context.inPacket().receivedFrom(); |
| 587 | |
Phaneendra Manda | 2360d25 | 2016-06-15 18:53:30 +0530 | [diff] [blame^] | 588 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(PortNumber.TABLE).build(); |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 589 | OutboundPacket packet = new DefaultOutboundPacket(sourcePoint.deviceId(), treatment, context.inPacket() |
| 590 | .unparsed()); |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 591 | packetService.emit(packet); |
| 592 | log.trace("Sending packet: {}", packet); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Encapsulate 5 bit load balance id to nsh spi. |
| 598 | * |
| 599 | * @param id load balance identifier |
| 600 | * @param nshSpiId nsh service path index |
| 601 | * @return updated service path index |
| 602 | */ |
| 603 | protected int getNshServicePathId(LoadBalanceId id, int nshSpiId) { |
| 604 | int nshSpiNew = nshSpiId << 5; |
| 605 | nshSpiNew = nshSpiNew | id.loadBalanceId(); |
| 606 | return nshSpiNew; |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 607 | } |
Phaneendra Manda | 065d618 | 2016-05-26 20:10:21 +0530 | [diff] [blame] | 608 | |
| 609 | private void addToPortChainIdFiveTupleMap(PortChainId portChainId, FiveTuple fiveTuple) { |
| 610 | List<FiveTuple> list = portChainFiveTupleMap.get(portChainId); |
| 611 | list.add(fiveTuple); |
| 612 | portChainFiveTupleMap.put(portChainId, list); |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * Find the load balanced path set it to port chain for the given five |
| 617 | * tuple. |
| 618 | * |
| 619 | * @param portChainId port chain id |
| 620 | * @param fiveTuple five tuple info |
| 621 | * @return load balance id |
| 622 | */ |
| 623 | private LoadBalanceId loadBalanceSfc(PortChainId portChainId, FiveTuple fiveTuple) { |
| 624 | |
| 625 | // Get the port chain |
| 626 | PortChain portChain = portChainService.getPortChain(portChainId); |
| 627 | List<PortPairId> loadBalancePath = Lists.newArrayList(); |
| 628 | LoadBalanceId id; |
| 629 | int paths = portChain.getLoadBalancePathSize(); |
| 630 | if (paths >= MAX_LOAD_BALANCE_ID) { |
| 631 | log.info("Max limit reached for load balance paths. " |
| 632 | + "Reusing the created path for port chain {} with five tuple {}", portChainId, fiveTuple); |
| 633 | id = LoadBalanceId.of((byte) ((paths + 1) % MAX_LOAD_BALANCE_ID)); |
| 634 | portChain.addLoadBalancePath(fiveTuple, id, portChain.getLoadBalancePath(id)); |
| 635 | } |
| 636 | |
| 637 | // Get the list of port pair groups from port chain |
| 638 | Iterable<PortPairGroupId> portPairGroups = portChain.portPairGroups(); |
| 639 | for (final PortPairGroupId portPairGroupId : portPairGroups) { |
| 640 | PortPairGroup portPairGroup = portPairGroupService.getPortPairGroup(portPairGroupId); |
| 641 | |
| 642 | // Get the list of port pair ids from port pair group. |
| 643 | Iterable<PortPairId> portPairs = portPairGroup.portPairs(); |
| 644 | int minLoad = 0xFFF; |
| 645 | PortPairId minLoadPortPairId = null; |
| 646 | for (final PortPairId portPairId : portPairs) { |
| 647 | int load = portPairGroup.getLoad(portPairId); |
| 648 | if (load == 0) { |
| 649 | minLoadPortPairId = portPairId; |
| 650 | break; |
| 651 | } else { |
| 652 | // Check the port pair which has min load. |
| 653 | if (load < minLoad) { |
| 654 | minLoad = load; |
| 655 | minLoadPortPairId = portPairId; |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | if (minLoadPortPairId != null) { |
| 660 | loadBalancePath.add(minLoadPortPairId); |
| 661 | portPairGroup.addLoad(minLoadPortPairId); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | // Check if the path already exists, if not create a new id |
| 666 | id = portChain.matchPath(loadBalancePath); |
| 667 | if (id == null) { |
| 668 | id = LoadBalanceId.of((byte) (paths + 1)); |
| 669 | } |
| 670 | |
| 671 | portChain.addLoadBalancePath(fiveTuple, id, loadBalancePath); |
| 672 | return id; |
| 673 | } |
| 674 | |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 675 | } |