> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/smc/3.12/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.12/operation/session-handling.md).

# Session Handling

For updates of the PGW or other critical components, the impact should be
reduced by having as little sessions as possible on the PGW. To achieve this,
requests for new sessions can be rejected. When devices detach, the
number of sessions will decrease. In addition, sessions can be
actively terminated.

Both actions are triggered through API. To access the API, use a port-forward, for example:

```bash
kubectl port-forward <smc-pod> 8080:8080
```

## Reject New Contexts/Sessions

Check the internal state to accept new sessions through the API:

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

Change the internal state to accept new sessions:

```bash
$ curl -X POST "http://localhost:8080/api/v1/status/accept-new/false" -H "accept: application/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.

Drain existing sessions:

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

Drain existing sessions in a loop:

```bash
while true; do curl -m 10 -X DELETE "http://localhost:8080/api/v1/contexts/100" -H "accept: application/json"; sleep 5; done
{"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
11> ergw_api:delete_contexts(all).
ok
12> ergw_api:delete_contexts(10).
ok
```

## Get TDF Sessions

>**FEATURE STATE:** alpha

Obtain contexts per type from an Erlang shell:

```erlang
6> ergw_api:contexts(tdf).
ok
7> ergw_api:contexts('gtp-c').
ok
```

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
7> ergw_api:contexts(all).
ok
```

## Drain TDF Sessions

>**FEATURE STATE:** alpha

Drain sessions per type from an Erlang shell:

```erlang
6> ergw_api:delete_contexts(tdf, 10).
ok
7> ergw_api:delete_contexts(tdf, all).
ok
9> ergw_api:delete_contexts('gtp-c', 10).
ok
10> ergw_api:delete_contexts('gtp-c', all).
ok
```

>**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 `ergw_sx_node_reg:all()`:

```erlang
1> ergw_sx_node_reg:all().
[{{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
2> ergw_sx_node:down({127,0,200,1}).
ok
3> ergw_sx_node:down(<<"topon.sx.prox03.epc.mnc001.mcc001.3gppnetwork.org">>).
ok
```

>**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.
