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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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 | 8db7d09 | 2016-06-04 00:17:24 +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); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 319 | nshSpi = NshServicePathId.of(getNshServicePathId(id, nshSpiId)); |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 320 | if (processedIdList.contains(id)) { |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 321 | // Multiple five tuple can have single path. In this case only |
| 322 | // the classifier rule need to delete |
| 323 | flowRuleInstaller.unInstallLoadBalancedClassifierRules(portChain, fiveTuple, nshSpi); |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 324 | continue; |
| 325 | } else { |
| 326 | processedIdList.add(id); |
| 327 | } |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 328 | flowRuleInstaller.unInstallLoadBalancedFlowRules(portChain, fiveTuple, nshSpi); |
| 329 | } |
| 330 | |
| 331 | // Reset load for all the port pairs |
| 332 | List<PortPairGroupId> ppgIdlist = portChain.portPairGroups(); |
| 333 | ListIterator<PortPairGroupId> ppgIdListIterator = ppgIdlist.listIterator(); |
| 334 | while (ppgIdListIterator.hasNext()) { |
| 335 | PortPairGroupId portPairGroupId = ppgIdListIterator.next(); |
| 336 | PortPairGroup ppg = portPairGroupService.getPortPairGroup(portPairGroupId); |
| 337 | ppg.resetLoad(); |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Get next nsh service path identifier. |
| 343 | * |
| 344 | * @return value of service path identifier |
| 345 | */ |
| 346 | int getNextNshSpi() { |
| 347 | // If there is any free id use it. Otherwise generate new id. |
| 348 | if (nshSpiIdFreeList.isEmpty()) { |
| 349 | return (int) nshSpiIdGenerator.getNewId(); |
| 350 | } |
| 351 | Iterator<Integer> it = nshSpiIdFreeList.iterator(); |
| 352 | Integer value = it.next(); |
| 353 | nshSpiIdFreeList.remove(value); |
| 354 | return value; |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 355 | } |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 356 | |
| 357 | private class SfcPacketProcessor implements PacketProcessor { |
| 358 | |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 359 | /** |
| 360 | * Check for given ip match with the fixed ips for the virtual port. |
| 361 | * |
| 362 | * @param vPortId virtual port id |
| 363 | * @param ip ip address to match |
| 364 | * @return true if the ip match with the fixed ips in virtual port false otherwise |
| 365 | */ |
| 366 | private boolean checkIpInVirtualPort(VirtualPortId vPortId, IpAddress ip) { |
| 367 | boolean found = false; |
| 368 | Set<FixedIp> ips = virtualPortService.getPort(vPortId).fixedIps(); |
| 369 | for (FixedIp fixedIp : ips) { |
| 370 | if (fixedIp.ip().equals(ip)) { |
| 371 | found = true; |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | return found; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Find the port chain for the received packet. |
| 380 | * |
| 381 | * @param fiveTuple five tuple info from the packet |
| 382 | * @return portChainId id of port chain |
| 383 | */ |
| 384 | private PortChainId findPortChainFromFiveTuple(FiveTuple fiveTuple) { |
| 385 | |
| 386 | PortChainId portChainId = null; |
| 387 | |
| 388 | Iterable<PortChain> portChains = portChainService.getPortChains(); |
| 389 | if (portChains == null) { |
| 390 | log.error("Could not retrive port chain list"); |
| 391 | } |
| 392 | |
| 393 | // Identify the port chain to which the packet belongs |
| 394 | for (final PortChain portChain : portChains) { |
| 395 | |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 396 | if (!portChain.tenantId().equals(fiveTuple.tenantId())) { |
| 397 | continue; |
| 398 | } |
| 399 | |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 400 | Iterable<FlowClassifierId> flowClassifiers = portChain.flowClassifiers(); |
| 401 | |
| 402 | // One port chain can have multiple flow classifiers. |
| 403 | for (final FlowClassifierId flowClassifierId : flowClassifiers) { |
| 404 | |
| 405 | FlowClassifier flowClassifier = flowClassifierService.getFlowClassifier(flowClassifierId); |
| 406 | boolean match = false; |
| 407 | // Check whether protocol is set in flow classifier |
| 408 | if (flowClassifier.protocol() != null) { |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 409 | if ((flowClassifier.protocol().equalsIgnoreCase("TCP") |
| 410 | && fiveTuple.protocol() == IPv4.PROTOCOL_TCP) |
| 411 | || (flowClassifier.protocol().equalsIgnoreCase("UDP") |
| 412 | && fiveTuple.protocol() == IPv4.PROTOCOL_UDP) |
| 413 | || (flowClassifier.protocol().equalsIgnoreCase("ICMP") |
| 414 | && fiveTuple.protocol() == IPv4.PROTOCOL_ICMP)) { |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 415 | match = true; |
| 416 | } else { |
| 417 | continue; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | // Check whether source ip prefix is set in flow classifier |
| 422 | if (flowClassifier.srcIpPrefix() != null) { |
| 423 | if (flowClassifier.srcIpPrefix().contains(fiveTuple.ipSrc())) { |
| 424 | match = true; |
| 425 | } else { |
| 426 | continue; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // Check whether destination ip prefix is set in flow classifier |
| 431 | if (flowClassifier.dstIpPrefix() != null) { |
| 432 | if (flowClassifier.dstIpPrefix().contains(fiveTuple.ipDst())) { |
| 433 | match = true; |
| 434 | } else { |
| 435 | continue; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | // Check whether source port is set in flow classifier |
| 440 | if (fiveTuple.portSrc().toLong() >= flowClassifier.minSrcPortRange() || |
| 441 | fiveTuple.portSrc().toLong() <= flowClassifier.maxSrcPortRange()) { |
| 442 | match = true; |
| 443 | } else { |
| 444 | continue; |
| 445 | } |
| 446 | |
| 447 | // Check whether destination port is set in flow classifier |
| 448 | if (fiveTuple.portDst().toLong() >= flowClassifier.minSrcPortRange() || |
| 449 | fiveTuple.portDst().toLong() <= flowClassifier.maxSrcPortRange()) { |
| 450 | match = true; |
| 451 | } else { |
| 452 | continue; |
| 453 | } |
| 454 | |
| 455 | // Check whether neutron source port is set in flow classfier |
| 456 | if ((flowClassifier.srcPort() != null) && (!flowClassifier.srcPort().portId().isEmpty())) { |
| 457 | match = checkIpInVirtualPort(VirtualPortId.portId(flowClassifier.srcPort().portId()), |
| 458 | fiveTuple.ipSrc()); |
| 459 | if (!match) { |
| 460 | continue; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // Check whether destination neutron destination port is set in flow classifier |
| 465 | if ((flowClassifier.dstPort() != null) && (!flowClassifier.dstPort().portId().isEmpty())) { |
| 466 | match = checkIpInVirtualPort(VirtualPortId.portId(flowClassifier.dstPort().portId()), |
| 467 | fiveTuple.ipDst()); |
| 468 | if (!match) { |
| 469 | continue; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | if (match) { |
| 474 | portChainId = portChain.portChainId(); |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | return portChainId; |
| 480 | } |
| 481 | |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 482 | /** |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 483 | * Get the tenant id for the given mac address. |
| 484 | * |
| 485 | * @param mac mac address |
| 486 | * @return tenantId tenant id for the given mac address |
| 487 | */ |
| 488 | private TenantId getTenantId(MacAddress mac) { |
| 489 | Collection<VirtualPort> virtualPorts = virtualPortService.getPorts(); |
| 490 | for (VirtualPort virtualPort : virtualPorts) { |
| 491 | if (virtualPort.macAddress().equals(mac)) { |
| 492 | return virtualPort.tenantId(); |
| 493 | } |
| 494 | } |
| 495 | return null; |
| 496 | } |
| 497 | |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 498 | @Override |
| 499 | public void process(PacketContext context) { |
| 500 | Ethernet packet = context.inPacket().parsed(); |
Phaneendra Manda | 089274b | 2016-04-11 17:21:56 +0530 | [diff] [blame] | 501 | if (packet == null || portChainService.getPortChainCount() == 0) { |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 502 | return; |
| 503 | } |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 504 | // get the five tuple parameters for the packet |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 505 | short ethType = packet.getEtherType(); |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 506 | IpAddress ipSrc = null; |
| 507 | IpAddress ipDst = null; |
| 508 | int portSrc = 0; |
| 509 | int portDst = 0; |
| 510 | byte protocol = 0; |
| 511 | MacAddress macSrc = packet.getSourceMAC(); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 512 | MacAddress macDst = packet.getDestinationMAC(); |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 513 | TenantId tenantId = getTenantId(macSrc); |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 514 | |
| 515 | if (ethType == Ethernet.TYPE_IPV4) { |
| 516 | IPv4 ipv4Packet = (IPv4) packet.getPayload(); |
| 517 | ipSrc = IpAddress.valueOf(ipv4Packet.getSourceAddress()); |
| 518 | ipDst = IpAddress.valueOf(ipv4Packet.getDestinationAddress()); |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 519 | protocol = ipv4Packet.getProtocol(); |
| 520 | if (protocol == IPv4.PROTOCOL_TCP) { |
| 521 | TCP tcpPacket = (TCP) ipv4Packet.getPayload(); |
| 522 | portSrc = tcpPacket.getSourcePort(); |
| 523 | portDst = tcpPacket.getDestinationPort(); |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 524 | } else if (protocol == IPv4.PROTOCOL_UDP) { |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 525 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 526 | portSrc = udpPacket.getSourcePort(); |
| 527 | portDst = udpPacket.getDestinationPort(); |
Phaneendra Manda | 089274b | 2016-04-11 17:21:56 +0530 | [diff] [blame] | 528 | } else if (protocol == IPv4.PROTOCOL_ICMP) { |
| 529 | // do nothing |
| 530 | } else { |
| 531 | // No need to process other packets received by controller. |
| 532 | return; |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 533 | } |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 534 | } else { |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 535 | return; |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 536 | } |
| 537 | |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 538 | FiveTuple fiveTuple = DefaultFiveTuple.builder() |
| 539 | .setIpSrc(ipSrc) |
| 540 | .setIpDst(ipDst) |
| 541 | .setPortSrc(PortNumber.portNumber(portSrc)) |
| 542 | .setPortDst(PortNumber.portNumber(portDst)) |
| 543 | .setProtocol(protocol) |
| 544 | .setTenantId(tenantId) |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 545 | .setMacSrc(macSrc) |
| 546 | .setMacDst(macDst) |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 547 | .build(); |
| 548 | |
| 549 | PortChainId portChainId = findPortChainFromFiveTuple(fiveTuple); |
| 550 | |
| 551 | if (portChainId == null) { |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 552 | return; |
| 553 | } |
| 554 | |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 555 | // Once the 5 tuple and port chain are identified, give this input for load balancing |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 556 | addToPortChainIdFiveTupleMap(portChainId, fiveTuple); |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 557 | LoadBalanceId id = loadBalanceSfc(portChainId, fiveTuple); |
| 558 | // Get nsh service path index |
| 559 | NshServicePathId nshSpi; |
| 560 | PortChain portChain = portChainService.getPortChain(portChainId); |
| 561 | if (nshSpiPortChainMap.containsKey(portChain.portChainId())) { |
| 562 | int nshSpiId = nshSpiPortChainMap.get(portChain.portChainId()); |
| 563 | nshSpi = NshServicePathId.of(getNshServicePathId(id, nshSpiId)); |
| 564 | } else { |
| 565 | int nshSpiId = getNextNshSpi(); |
| 566 | if (nshSpiId > MAX_NSH_SPI_ID) { |
| 567 | log.error("Reached max limit of service path index." |
| 568 | + "Failed to install SFC for port chain {}", portChain.portChainId()); |
| 569 | return; |
| 570 | } |
| 571 | nshSpi = NshServicePathId.of(getNshServicePathId(id, nshSpiId)); |
| 572 | nshSpiPortChainMap.put(portChain.portChainId(), new Integer(nshSpiId)); |
| 573 | } |
Phaneendra Manda | b355503 | 2016-02-08 11:41:53 +0530 | [diff] [blame] | 574 | // download the required flow rules for classifier and forwarding |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 575 | // install in OVS. |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 576 | SfcFlowRuleInstallerImpl flowRuleInstaller = new SfcFlowRuleInstallerImpl(appId); |
| 577 | flowRuleInstaller.installLoadBalancedFlowRules(portChain, fiveTuple, nshSpi); |
| 578 | sendPacket(context); |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 579 | } |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 580 | |
| 581 | /** |
| 582 | * Send packet back to classifier. |
| 583 | * |
| 584 | * @param context packet context |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 585 | */ |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 586 | private void sendPacket(PacketContext context) { |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 587 | |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 588 | ConnectPoint sourcePoint = context.inPacket().receivedFrom(); |
| 589 | |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 590 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(PortNumber.TABLE).build(); |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 591 | OutboundPacket packet = new DefaultOutboundPacket(sourcePoint.deviceId(), treatment, context.inPacket() |
| 592 | .unparsed()); |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 593 | packetService.emit(packet); |
| 594 | log.trace("Sending packet: {}", packet); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Encapsulate 5 bit load balance id to nsh spi. |
| 600 | * |
| 601 | * @param id load balance identifier |
| 602 | * @param nshSpiId nsh service path index |
| 603 | * @return updated service path index |
| 604 | */ |
| 605 | protected int getNshServicePathId(LoadBalanceId id, int nshSpiId) { |
| 606 | int nshSpiNew = nshSpiId << 5; |
| 607 | nshSpiNew = nshSpiNew | id.loadBalanceId(); |
| 608 | return nshSpiNew; |
Phaneendra Manda | 5c8257b | 2016-02-03 22:07:38 +0530 | [diff] [blame] | 609 | } |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 610 | |
| 611 | private void addToPortChainIdFiveTupleMap(PortChainId portChainId, FiveTuple fiveTuple) { |
| 612 | List<FiveTuple> list = portChainFiveTupleMap.get(portChainId); |
| 613 | list.add(fiveTuple); |
| 614 | portChainFiveTupleMap.put(portChainId, list); |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * Find the load balanced path set it to port chain for the given five |
| 619 | * tuple. |
| 620 | * |
| 621 | * @param portChainId port chain id |
| 622 | * @param fiveTuple five tuple info |
| 623 | * @return load balance id |
| 624 | */ |
| 625 | private LoadBalanceId loadBalanceSfc(PortChainId portChainId, FiveTuple fiveTuple) { |
| 626 | |
| 627 | // Get the port chain |
| 628 | PortChain portChain = portChainService.getPortChain(portChainId); |
| 629 | List<PortPairId> loadBalancePath = Lists.newArrayList(); |
| 630 | LoadBalanceId id; |
| 631 | int paths = portChain.getLoadBalancePathSize(); |
| 632 | if (paths >= MAX_LOAD_BALANCE_ID) { |
| 633 | log.info("Max limit reached for load balance paths. " |
| 634 | + "Reusing the created path for port chain {} with five tuple {}", portChainId, fiveTuple); |
| 635 | id = LoadBalanceId.of((byte) ((paths + 1) % MAX_LOAD_BALANCE_ID)); |
| 636 | portChain.addLoadBalancePath(fiveTuple, id, portChain.getLoadBalancePath(id)); |
| 637 | } |
| 638 | |
| 639 | // Get the list of port pair groups from port chain |
| 640 | Iterable<PortPairGroupId> portPairGroups = portChain.portPairGroups(); |
| 641 | for (final PortPairGroupId portPairGroupId : portPairGroups) { |
| 642 | PortPairGroup portPairGroup = portPairGroupService.getPortPairGroup(portPairGroupId); |
| 643 | |
| 644 | // Get the list of port pair ids from port pair group. |
| 645 | Iterable<PortPairId> portPairs = portPairGroup.portPairs(); |
| 646 | int minLoad = 0xFFF; |
| 647 | PortPairId minLoadPortPairId = null; |
| 648 | for (final PortPairId portPairId : portPairs) { |
| 649 | int load = portPairGroup.getLoad(portPairId); |
| 650 | if (load == 0) { |
| 651 | minLoadPortPairId = portPairId; |
| 652 | break; |
| 653 | } else { |
| 654 | // Check the port pair which has min load. |
| 655 | if (load < minLoad) { |
| 656 | minLoad = load; |
| 657 | minLoadPortPairId = portPairId; |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | if (minLoadPortPairId != null) { |
| 662 | loadBalancePath.add(minLoadPortPairId); |
| 663 | portPairGroup.addLoad(minLoadPortPairId); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | // Check if the path already exists, if not create a new id |
| 668 | id = portChain.matchPath(loadBalancePath); |
| 669 | if (id == null) { |
| 670 | id = LoadBalanceId.of((byte) (paths + 1)); |
| 671 | } |
| 672 | |
| 673 | portChain.addLoadBalancePath(fiveTuple, id, loadBalancePath); |
| 674 | return id; |
| 675 | } |
| 676 | |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 677 | } |