Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.pipelines.fabric.impl.behaviour.upf; |
| 18 | |
| 19 | import com.google.common.collect.BiMap; |
| 20 | import com.google.common.collect.ImmutableBiMap; |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 21 | import com.google.common.hash.HashCode; |
| 22 | import com.google.common.hash.HashFunction; |
| 23 | import com.google.common.hash.Hashing; |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 24 | import org.onlab.util.ImmutableByteSequence; |
| 25 | import org.onlab.util.KryoNamespace; |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 26 | import org.onosproject.store.serializers.KryoNamespaces; |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 27 | import org.onosproject.store.service.EventuallyConsistentMap; |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 28 | import org.onosproject.store.service.StorageService; |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 29 | import org.onosproject.store.service.WallClockTimestamp; |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 30 | import org.osgi.service.component.annotations.Activate; |
| 31 | import org.osgi.service.component.annotations.Component; |
| 32 | import org.osgi.service.component.annotations.Deactivate; |
| 33 | import org.osgi.service.component.annotations.Reference; |
| 34 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 38 | import java.util.Map; |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 39 | import java.util.stream.Collectors; |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Distributed implementation of FabricUpfStore. |
| 43 | */ |
| 44 | // FIXME: this store is generic and not tied to a single device, should we have a store based on deviceId? |
| 45 | @Component(immediate = true, service = FabricUpfStore.class) |
| 46 | public final class DistributedFabricUpfStore implements FabricUpfStore { |
| 47 | |
| 48 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 49 | |
| 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 51 | protected StorageService storageService; |
| 52 | |
| 53 | protected static final String FAR_ID_MAP_NAME = "fabric-upf-far-id"; |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 54 | protected static final KryoNamespace.Builder SERIALIZER = KryoNamespace.newBuilder() |
| 55 | .register(KryoNamespaces.API) |
| 56 | .register(UpfRuleIdentifier.class); |
| 57 | |
| 58 | // TODO: check queue IDs for BMv2, is priority inverted? |
| 59 | // Mapping between scheduling priority ranges with BMv2 priority queues |
| 60 | private static final BiMap<Integer, Integer> SCHEDULING_PRIORITY_MAP |
| 61 | = new ImmutableBiMap.Builder<Integer, Integer>() |
| 62 | // Highest scheduling priority for 3GPP is 1 and highest BMv2 queue priority is 7 |
| 63 | .put(1, 5) |
| 64 | .put(6, 4) |
| 65 | .put(7, 3) |
| 66 | .put(8, 2) |
| 67 | .put(9, 1) |
| 68 | .build(); |
| 69 | |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 70 | // EC map to remember the mapping far_id -> rule_id this is mostly used during reads, |
| 71 | // it can be definitely removed by simplifying the logical pipeline |
| 72 | protected EventuallyConsistentMap<Integer, UpfRuleIdentifier> reverseFarIdMap; |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 73 | |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 74 | @Activate |
| 75 | protected void activate() { |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 76 | // Allow unit test to inject reverseFarIdMap here. |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 77 | if (storageService != null) { |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 78 | this.reverseFarIdMap = storageService.<Integer, UpfRuleIdentifier>eventuallyConsistentMapBuilder() |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 79 | .withName(FAR_ID_MAP_NAME) |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 80 | .withSerializer(SERIALIZER) |
| 81 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 82 | .build(); |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 83 | } |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 84 | |
| 85 | log.info("Started"); |
| 86 | } |
| 87 | |
| 88 | @Deactivate |
| 89 | protected void deactivate() { |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 90 | reverseFarIdMap.destroy(); |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 91 | |
| 92 | log.info("Stopped"); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void reset() { |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 97 | reverseFarIdMap.clear(); |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 101 | public Map<Integer, UpfRuleIdentifier> getReverseFarIdMap() { |
| 102 | return reverseFarIdMap.entrySet().stream() |
| 103 | .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public int globalFarIdOf(UpfRuleIdentifier farIdPair) { |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 108 | int globalFarId = getGlobalFarIdOf(farIdPair); |
| 109 | reverseFarIdMap.put(globalFarId, farIdPair); |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 110 | log.info("{} translated to GlobalFarId={}", farIdPair, globalFarId); |
| 111 | return globalFarId; |
| 112 | } |
| 113 | |
| 114 | @Override |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 115 | public int removeGlobalFarId(UpfRuleIdentifier farIdPair) { |
| 116 | int globalFarId = getGlobalFarIdOf(farIdPair); |
| 117 | reverseFarIdMap.remove(globalFarId); |
| 118 | return globalFarId; |
| 119 | } |
| 120 | |
| 121 | @Override |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 122 | public int globalFarIdOf(ImmutableByteSequence pfcpSessionId, int sessionLocalFarId) { |
| 123 | UpfRuleIdentifier farId = new UpfRuleIdentifier(pfcpSessionId, sessionLocalFarId); |
| 124 | return globalFarIdOf(farId); |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 125 | } |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 126 | |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 127 | @Override |
| 128 | public int removeGlobalFarId(ImmutableByteSequence pfcpSessionId, int sessionLocalFarId) { |
| 129 | UpfRuleIdentifier farId = new UpfRuleIdentifier(pfcpSessionId, sessionLocalFarId); |
| 130 | return removeGlobalFarId(farId); |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public String queueIdOf(int schedulingPriority) { |
| 135 | return (SCHEDULING_PRIORITY_MAP.get(schedulingPriority)).toString(); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public String schedulingPriorityOf(int queueId) { |
| 140 | return (SCHEDULING_PRIORITY_MAP.inverse().get(queueId)).toString(); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public UpfRuleIdentifier localFarIdOf(int globalFarId) { |
| 145 | return reverseFarIdMap.get(globalFarId); |
| 146 | } |
| 147 | |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 148 | // Compute global far id by hashing the pfcp session id and the session local far |
| 149 | private int getGlobalFarIdOf(UpfRuleIdentifier farIdPair) { |
| 150 | HashFunction hashFunction = Hashing.murmur3_32(); |
| 151 | HashCode hashCode = hashFunction.newHasher() |
| 152 | .putInt(farIdPair.getSessionLocalId()) |
| 153 | .putBytes(farIdPair.getPfcpSessionId().asArray()) |
| 154 | .hash(); |
| 155 | return hashCode.asInt(); |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 156 | } |
pierventre | 1eb9871 | 2021-07-13 18:03:22 +0200 | [diff] [blame^] | 157 | |
Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame] | 158 | } |