> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/smc/3.14/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/smc/3.14/operation/session-handling.md).

# Session handling

When updating the Packet Gateway (PGW) or other critical components, it’s important to minimize active sessions on the PGW to reduce service impact. This document explains how to manage sessions during such operations by:

- Rejecting new session requests to prevent additional load.
- Draining existing sessions, allowing them to end naturally without accepting new ones.
- Actively terminating sessions to immediately free up resources or speed up maintenance.

You can perform these actions through the system’s API. To access the API, start by setting up a port-forward to the target pod, for example:

```bash
kubectl port-forward {SMC_POD_NAME} 8080:8080
```

## Reject new sessions

You can check and update the internal state of the service to control whether it accepts or reject new sessions through the API. Run this command to reject new sessions and lower the impact on PGW:

```bash
curl -X GET "http://localhost:8080/api/v1/status/accept-new" -H "accept: application/json"
```

As you can see in the output, new sessions are accepted:

```json
{"acceptNewRequests":true}
```

Run this command to change the state and reject new sessions:

```bash
curl -X POST "http://localhost:8080/api/v1/status/accept-new/false" -H "accept: application/json"
```

Now, the output shows that new sessions are rejected:

```json
{"acceptNewRequests":false}
```

## Drain sessions

> [!CAUTION]
> Draining sessions increases the memory usage that mainly depends on the current number of sessions and **not** on the number of sessions to delete (around 2.6GB for 250k current sessions). Remove a large number of sessions (for example, 2000) at once instead of repeatedly removing a small number of sessions.

Run this command to drain the existing sessions:

```bash
curl -X DELETE "http://localhost:8080/api/v1/contexts/100" -H "accept: application/json"
```

As you can see in the output example, there are 865 active contexts (sessions) remaining in the system after processing the `DELETE` request:

```json
{"contexts":865}
```

You can also drain the existing sessions in a loop, which means continuously checking for active sessions and terminating them one by one (or in batches) until none are left. To do so, run:

```bash
while true; do curl -m 10 -X DELETE "http://localhost:8080/api/v1/contexts/100" -H "accept: application/json"; sleep 5; done
```

This command sends a `DELETE` request every 5 seconds. As we can see in the output example, after first run there are 765 sessions, after second run there are 665 sessions, and after third run - 565 sessions:

```json
{"contexts":765}{"contexts":665}{"contexts":565}
```

Using the `ergw` Erlang API without a specific type refers to contexts of all types. The following commands delete both GTP-initiated and TDF (PFCP-initiated) contexts:

```erlang
ergw_api:delete_contexts(all).
ergw_api:delete_contexts(10).
```

## Get TDF sessions

>**FEATURE STATE:** alpha

Obtain contexts per type from an Erlang shell:

```erlang
ergw_api:contexts(tdf).
ergw_api:contexts('gtp-c').
```

Using the `ergw` Erlang API without a specific type refers to contexts of all types. The following commands delete both GTP-initiated and TDF (PFCP-initiated) contexts:

```erlang
ergw_api:contexts(all).
```

## Drain TDF Sessions

>**FEATURE STATE:** alpha

Drain sessions per type from an Erlang shell:

```erlang
ergw_api:delete_contexts(tdf, 10).
ergw_api:delete_contexts(tdf, all).
ergw_api:delete_contexts('gtp-c', 10).
ergw_api:delete_contexts('gtp-c', all).
```

> [!CAUTION]
> The following script can be used at an Erlang shell to manually remove TDF contexts in previous releases. **Incorrect usage may result in session termination without AAA termination and may cause a node reboot.**

`Split` is provided due to the limitations of `lists:split/2`.

```erlang
%% helper functions
TdfContexts = fun () ->
    lists:usort([Pid || {{ue, Vrf, Addr}, {_, Pid}} <- gtp_context_reg:all(), is_pid(Pid)]) end.
DeleteTdfContext = fun (Context) ->
    Context ! {update_session, #{}, [{stop, administrative}]} end.
DeleteTdfContexts = fun (Contexts) ->
    lists:foreach(DeleteTdfContext, Contexts) end.
Split = fun (Contexts, N) when N > length(Contexts) ->
    {Contexts, []};
  (Contexts, N) when N =< length(Contexts) ->
    lists:split(N, Contexts) end.
DrainTdfContexts = fun DrainTdfContexts({Contexts, []}, _N, SleepTime) ->
                           DeleteTdfContexts(Contexts);
                       DrainTdfContexts({Contexts, Rest}, N) ->
                           DeleteTdfContexts(Contexts),
                           timer:sleep(SleepTime),
                           DrainTdfContexts(Split(Rest, N), N, SleepTime)
                   end.
DrainTdfContexts(Split(TdfContexts(), 2), 2, 100).
```

## List and close PFCP associations

All existing PFCP associations can be found through the Sx node registry using this command:

```erlang
ergw_sx_node_reg:all().
```

See the output example:

```erlang
[{{127,0,200,1},<0.3179.0>},
 {{127,0,200,2},<0.3191.0>},
 {<<"topon.sx.prox01.epc.mnc001.mcc001.3gppnetwork.org">>,
  <0.3179.0>},
 {<<"topon.sx.prox03.epc.mnc001.mcc001.3gppnetwork.org">>,
  <0.3191.0>}]
```

A single PFCP association can be removed through `ergw_sx_node:down(Key)`, where `Key` is either the PID of the Sx process or any other key the association is registered with. In the example above, the key can be either the linked PID (for example `<0.3179.0>`), the IP of the PFCP peer in tuple form (for example `{127,0,200,1}`) or the FQDN of the PFCP peer in binary format (for example `<<"topon.sx.prox01.epc.mnc001.mcc001.3gppnetwork.org">>`).

```erlang
ergw_sx_node:down({127,0,200,1}).
ergw_sx_node:down(<<"topon.sx.prox03.epc.mnc001.mcc001.3gppnetwork.org">>).
```

> [!NOTE]
> Invoking `down` will behave as if a node down (no PFCP Echo response) were detected. All sessions that existed in that UPF will receive a down notification and be closed. The retry counter for PFCP Association Requests will also be reset to 0, triggering an immediate reconnection attempt.
