> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/smc/3.10/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.10/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 via API. To access the API, use a port-forward, e.g.:

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

## Reject New Contexts/Sessions

Check the internal state to accept new sessions via API:

```
$ 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:

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

## Drain Sessions

Drain existing sessions:

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

Drain existing sessions in a loop:

```
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).
```
