tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [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 com.google.common.annotations.Beta; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
| 26 | /** |
| 27 | * A structure representing the UE Session on the UPF-programmable device. |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 28 | * Provide means to set up the UPF UE Session in the uplink direction. |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 29 | */ |
| 30 | @Beta |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 31 | public final class UpfSessionUplink implements UpfEntity { |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 32 | // Match Keys |
| 33 | private final Ip4Address tunDestAddr; // The tunnel destination address (N3/S1U IPv4 address) |
| 34 | private final Integer teid; // The Tunnel Endpoint ID that this UeSession matches on |
| 35 | |
| 36 | // Action parameters |
| 37 | private final boolean dropping; // Used to convey dropping information |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 38 | private final int sessionMeterIdx; |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 39 | |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 40 | private UpfSessionUplink(Ip4Address tunDestAddr, |
| 41 | Integer teid, |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 42 | int sessionMeterIdx, |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 43 | boolean drop) { |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 44 | this.tunDestAddr = tunDestAddr; |
| 45 | this.teid = teid; |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 46 | this.sessionMeterIdx = sessionMeterIdx; |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 47 | this.dropping = drop; |
| 48 | } |
| 49 | |
| 50 | public static Builder builder() { |
| 51 | return new Builder(); |
| 52 | } |
| 53 | |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 54 | @Override |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 55 | public boolean equals(Object object) { |
| 56 | if (object == this) { |
| 57 | return true; |
| 58 | } |
| 59 | if (object == null) { |
| 60 | return false; |
| 61 | } |
| 62 | if (getClass() != object.getClass()) { |
| 63 | return false; |
| 64 | } |
| 65 | |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 66 | UpfSessionUplink that = (UpfSessionUplink) object; |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 67 | |
| 68 | return this.dropping == that.dropping && |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 69 | this.sessionMeterIdx == that.sessionMeterIdx && |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 70 | Objects.equals(tunDestAddr, that.tunDestAddr) && |
| 71 | Objects.equals(teid, that.teid); |
| 72 | } |
| 73 | |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 74 | @Override |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 75 | public int hashCode() { |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 76 | return Objects.hash(tunDestAddr, teid, sessionMeterIdx, dropping); |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public String toString() { |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 81 | return "UpfSessionUL(" + matchString() + " -> " + actionString() + ")"; |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | private String matchString() { |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 85 | return "Match(tun_dst_addr=" + this.tunDstAddr() + ", teid=" + this.teid() + ")"; |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | private String actionString() { |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 89 | StringBuilder actionStrBuilder = new StringBuilder("Action("); |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 90 | if (this.needsDropping()) { |
| 91 | actionStrBuilder.append("DROP"); |
| 92 | |
| 93 | } else { |
| 94 | actionStrBuilder.append("FWD"); |
| 95 | } |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 96 | return actionStrBuilder |
| 97 | .append(", session_meter_idx=").append(this.sessionMeterIdx()) |
| 98 | .append(")").toString(); |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 102 | * True if this UPF UE Session needs dropping of the uplink traffic. |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 103 | * |
| 104 | * @return true if the UE Session needs dropping. |
| 105 | */ |
| 106 | public boolean needsDropping() { |
| 107 | return dropping; |
| 108 | } |
| 109 | |
| 110 | /** |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 111 | * Get the tunnel destination IP address in the uplink UPF UE session (N3/S1U IP address). |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 112 | * |
| 113 | * @return UE IP address |
| 114 | */ |
| 115 | public Ip4Address tunDstAddr() { |
| 116 | return tunDestAddr; |
| 117 | } |
| 118 | |
| 119 | /** |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 120 | * Get the identifier of the GTP tunnel that this UPF UE Session rule matches on. |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 121 | * |
| 122 | * @return GTP tunnel ID |
| 123 | */ |
| 124 | public Integer teid() { |
| 125 | return teid; |
| 126 | } |
| 127 | |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 128 | /** |
| 129 | * Get the session meter index that is set by this UPF UE Session rule. |
| 130 | * |
| 131 | * @return Session meter index |
| 132 | */ |
| 133 | public int sessionMeterIdx() { |
| 134 | return this.sessionMeterIdx; |
| 135 | } |
| 136 | |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 137 | @Override |
| 138 | public UpfEntityType type() { |
| 139 | return UpfEntityType.SESSION_UPLINK; |
| 140 | } |
| 141 | |
| 142 | public static class Builder { |
| 143 | private Ip4Address tunDstAddr = null; |
| 144 | private Integer teid = null; |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 145 | public int sessionMeterIdx = DEFAULT_SESSION_INDEX; |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 146 | private boolean drop = false; |
| 147 | |
| 148 | public Builder() { |
| 149 | |
| 150 | } |
| 151 | |
| 152 | /** |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 153 | * Sets the tunnel destination IP address (N3/S1U address) that this UPF UE Session rule matches on. |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 154 | * |
| 155 | * @param tunDstAddr The tunnel destination IP address |
| 156 | * @return This builder object |
| 157 | */ |
| 158 | public Builder withTunDstAddr(Ip4Address tunDstAddr) { |
| 159 | this.tunDstAddr = tunDstAddr; |
| 160 | return this; |
| 161 | } |
| 162 | |
| 163 | /** |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 164 | * Sets the identifier of the GTP tunnel that this UPF UE Session rule matches on. |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 165 | * |
| 166 | * @param teid GTP tunnel ID |
| 167 | * @return This builder object |
| 168 | */ |
| 169 | public Builder withTeid(Integer teid) { |
| 170 | this.teid = teid; |
| 171 | return this; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /** |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 176 | * Sets whether to drop uplink UPF UE session traffic or not. |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 177 | * |
| 178 | * @param drop True if request to buffer, false otherwise |
| 179 | * @return This builder object |
| 180 | */ |
| 181 | public Builder needsDropping(boolean drop) { |
| 182 | this.drop = drop; |
| 183 | return this; |
| 184 | } |
| 185 | |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 186 | /** |
| 187 | * Sets the meter index associated with this UE session. |
| 188 | * If not set, default to {@link UpfEntity#DEFAULT_SESSION_INDEX}. |
| 189 | * |
| 190 | * @param sessionMeterIdx Session meter index |
| 191 | * @return This builder object |
| 192 | */ |
| 193 | public Builder withSessionMeterIdx(int sessionMeterIdx) { |
| 194 | this.sessionMeterIdx = sessionMeterIdx; |
| 195 | return this; |
| 196 | } |
| 197 | |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 198 | public UpfSessionUplink build() { |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 199 | // Match keys are required. |
| 200 | checkNotNull(tunDstAddr, "Tunnel destination must be provided"); |
| 201 | checkNotNull(teid, "TEID must be provided"); |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 202 | return new UpfSessionUplink(tunDstAddr, teid, sessionMeterIdx, drop); |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } |