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