Daniele Moro | dbcffda | 2021-06-11 16:41:48 +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.net.behaviour.upf; |
| 18 | |
| 19 | import org.onlab.packet.Ip4Address; |
| 20 | import org.onlab.packet.Ip4Prefix; |
| 21 | |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
| 26 | /** |
| 27 | * A UPF device interface, such as a S1U or UE IP address pool. |
| 28 | */ |
| 29 | public final class UpfInterface { |
| 30 | private final Ip4Prefix prefix; |
| 31 | private final Type type; |
| 32 | |
| 33 | private UpfInterface(Ip4Prefix prefix, Type type) { |
| 34 | this.prefix = prefix; |
| 35 | this.type = type; |
| 36 | } |
| 37 | |
| 38 | public static Builder builder() { |
| 39 | return new Builder(); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public String toString() { |
| 44 | String typeStr; |
| 45 | if (type.equals(Type.ACCESS)) { |
| 46 | typeStr = "Access"; |
| 47 | } else if (type.equals(Type.CORE)) { |
| 48 | typeStr = "Core"; |
| 49 | } else if (type.equals(Type.DBUF)) { |
| 50 | typeStr = "Dbuf-Receiver"; |
| 51 | } else { |
| 52 | typeStr = "UNKNOWN"; |
| 53 | } |
| 54 | return String.format("Interface{%s, %s}", typeStr, prefix); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public boolean equals(Object obj) { |
| 59 | if (obj == this) { |
| 60 | return true; |
| 61 | } |
| 62 | if (obj == null) { |
| 63 | return false; |
| 64 | } |
| 65 | if (getClass() != obj.getClass()) { |
| 66 | return false; |
| 67 | } |
| 68 | UpfInterface that = (UpfInterface) obj; |
| 69 | return (this.type.equals(that.type) && |
| 70 | this.prefix.equals(that.prefix)); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public int hashCode() { |
| 75 | return Objects.hash(prefix, type); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Create a core-facing UPF Interface from the given address, which will be treated as a /32 prefix. |
| 80 | * |
| 81 | * @param address the address of the new core-facing interface |
| 82 | * @return a new UPF interface |
| 83 | */ |
| 84 | public static UpfInterface createS1uFrom(Ip4Address address) { |
| 85 | return builder().setAccess().setPrefix(Ip4Prefix.valueOf(address, 32)).build(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Create a core-facing UPF Interface from the given IP prefix. |
| 90 | * |
| 91 | * @param prefix the prefix of the new core-facing interface |
| 92 | * @return a new UPF interface |
| 93 | */ |
| 94 | public static UpfInterface createUePoolFrom(Ip4Prefix prefix) { |
| 95 | return builder().setCore().setPrefix(prefix).build(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Create a dbuf-receiving UPF interface from the given IP address. |
| 100 | * |
| 101 | * @param address the address of the dbuf-receiving interface |
| 102 | * @return a new UPF interface |
| 103 | */ |
| 104 | public static UpfInterface createDbufReceiverFrom(Ip4Address address) { |
| 105 | return UpfInterface.builder().setDbufReceiver().setAddress(address).build(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get the IP prefix of this interface. |
| 110 | * |
| 111 | * @return the interface prefix |
| 112 | */ |
| 113 | public Ip4Prefix prefix() { |
| 114 | return prefix; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Check if this UPF interface is for packets traveling from UEs. |
| 119 | * This will be true for S1U interface table entries. |
| 120 | * |
| 121 | * @return true if interface receives from access |
| 122 | */ |
| 123 | public boolean isAccess() { |
| 124 | return type == Type.ACCESS; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Check if this UPF interface is for packets traveling towards UEs. |
| 129 | * This will be true for UE IP address pool table entries. |
| 130 | * |
| 131 | * @return true if interface receives from core |
| 132 | */ |
| 133 | public boolean isCore() { |
| 134 | return type == Type.CORE; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /** |
| 139 | * Check if this UPF interface is for receiving buffered packets as they are released from the dbuf |
| 140 | * buffering device. |
| 141 | * |
| 142 | * @return true if interface receives from dbuf |
| 143 | */ |
| 144 | public boolean isDbufReceiver() { |
| 145 | return type == Type.DBUF; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Get the IPv4 prefix of this UPF interface. |
| 150 | * |
| 151 | * @return the interface prefix |
| 152 | */ |
| 153 | public Ip4Prefix getPrefix() { |
| 154 | return this.prefix; |
| 155 | } |
| 156 | |
| 157 | public enum Type { |
| 158 | /** |
| 159 | * Unknown UPF interface type. |
| 160 | */ |
| 161 | UNKNOWN, |
| 162 | |
| 163 | /** |
| 164 | * Interface that receives GTP encapsulated packets. |
| 165 | * This is the type of the S1U interface. |
| 166 | */ |
| 167 | ACCESS, |
| 168 | |
| 169 | /** |
| 170 | * Interface that receives unencapsulated packets from the core of the network. |
| 171 | * This is the type of UE IP address pool interfaces. |
| 172 | */ |
| 173 | CORE, |
| 174 | |
| 175 | /** |
| 176 | * Interface that receives buffered packets as they are drained from a dbuf device. |
| 177 | */ |
| 178 | DBUF |
| 179 | } |
| 180 | |
| 181 | public static class Builder { |
| 182 | private Ip4Prefix prefix; |
| 183 | private Type type; |
| 184 | |
| 185 | public Builder() { |
| 186 | type = Type.UNKNOWN; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Set the IPv4 prefix of this interface. |
| 191 | * |
| 192 | * @param prefix the interface prefix |
| 193 | * @return this builder object |
| 194 | */ |
| 195 | public Builder setPrefix(Ip4Prefix prefix) { |
| 196 | this.prefix = prefix; |
| 197 | return this; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Set the IPv4 prefix of this interface, by turning the given address into a /32 prefix. |
| 202 | * |
| 203 | * @param address the interface address that will become a /32 prefix |
| 204 | * @return this builder object |
| 205 | */ |
| 206 | public Builder setAddress(Ip4Address address) { |
| 207 | this.prefix = Ip4Prefix.valueOf(address, 32); |
| 208 | return this; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Make this an access-facing interface. |
| 213 | * |
| 214 | * @return this builder object |
| 215 | */ |
| 216 | public Builder setAccess() { |
| 217 | this.type = Type.ACCESS; |
| 218 | return this; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Make this a core-facing interface. |
| 223 | * |
| 224 | * @return this builder object |
| 225 | */ |
| 226 | public Builder setCore() { |
| 227 | this.type = Type.CORE; |
| 228 | return this; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Make this a dbuf-facing interface. |
| 233 | * |
| 234 | * @return this builder object |
| 235 | */ |
| 236 | public Builder setDbufReceiver() { |
| 237 | this.type = Type.DBUF; |
| 238 | return this; |
| 239 | } |
| 240 | |
| 241 | public UpfInterface build() { |
| 242 | checkNotNull(prefix); |
| 243 | return new UpfInterface(prefix, type); |
| 244 | } |
| 245 | } |
| 246 | } |