> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/tposs/6.15/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/tposs/6.15/configuration/ip-pools-profiles.md).

# IP pools and service profiles

This document describes how you can configure IP pools and service profiles in TPOSS.

## IP pools configuration

To allocate Framed-IP-Addresses, TPOSS uses IP pools. They are specified using these configuration options:

```yaml
ippools:
  # Name of the pool which defines a certain IP range where IPs are allocated from
  wan_ip_range:
    # Range of IP addresses
    range: 10.192.0.0-10.193.255.254
    # Number of sub-blocks into which the IP pool is split. The IP blocks are distributed over TPOSS instances within TPOSS cluster.
    blocks: 20
    # Number of blocks to activate initially per instance. Use this option to spread IP usage over the range.
    initial_active_blocks: 1
    # Percentage of busy IPs within the inactive block after which the IP block
    # that previously was marked as `inactive` is marked as `active` again and TPOSS continues to use it.
    reactivate: 95
    # Percentage of block usage after which the block is marked as `inactive`.
    # After the block is marked as `inactive`, TPOSS does not allocate IPs from this block anymore
    # but only populates it back when IPs are released.
    mark_inactive: 100
    # Percentage of used IPs from a given block after which TPOSS tries to pre-fetch a new block of IP addresses
    packed: 75
    # Boolean value that provides the ability to decrease the number of blocks.
    # TPOSS ignores the new number of blocks if the value of this parameter is set to `false` and the new number of blocks is lower than the previous one.
    # In such a case, the original number of blocks is used.
    allow_to_decrease_number_of_blocks: false

# Mapping between pool names and their selectors based on the RADIUS attribute and its value
ippools_selectors:
  wan_ip_range: "Called-Station-Id:apn.example.com"
```

- **wan_ip_range** - the name of a pool which defines an IP range from where IPs are allocated.
- **range** - the range of IP addresses.
- **blocks** - the number of sub-blocks into which the IP pool is split. The IP blocks are distributed over TPOSS instances within a cluster.
- **allow_to_decrease_number_of_blocks** - the boolean value that provides the ability to decrease the number of blocks. TPOSS ignores the new number of blocks if the value of **allow_to_decrease_number_of_blocks** is set to `false` and the new number of blocks is lower than the previous one. In such a case, the original number of blocks is used.
- **mark_inactive** - the percentage of block usage after which the block is marked as `inactive`. After the block is marked as `inactive`, TPOSS does not allocate IPs from this block anymore but only populates it back when IPs are released.
- **packed** - the percentage of used IPs from a given block after which TPOSS tries to pre-fetch a new block of IP addresses.
- **reactivate** -  the percentage of busy IPs within the inactive block after which the IP block that previously was marked as `inactive` is marked as `active` again and TPOSS continues to use it.
- **initial_active_blocks** - the number of blocks to activate initially per instance. You can use this option to spread IP usage over the range.

The **ippools_selectors** parameter contains the mapping between pool names and their selectors based on the RADIUS attribute and its value.

If TPOSS allocates IP addresses, set this configuration option to `false` to handle RADIUS Access-Request from PGW properly:

```yaml
radius:
  # Allows to create network sessions on RADIUS Accounting-Start and Diameter Gx requests
  ggsn_creation_on_accounting: false
```

By default, allocated Framed IP addresses are stored in the MySQL IP table. To reduce the number of database queries needed for the allocation of a Framed IP address, use this configuration option:

```yaml
sessions:
  # Allows to store allocated Framed IP addresses in the network session table
  store_ip_within_network_sessions: false
```

If the value of this option is set to `true`, the separate IP table is not used anymore and allocated IP addresses are stored only in the network session table.

> **NOTE:** The **sessions.store_ip_within_network_sessions** feature is experimental. The new behaviour is disabled by default to preserve backward compatibility with the previous versions of TPOSS.

### Change IP pools block size and count

> **CAUTION:** You may change the IP pool block count only when the system is not under heavy load as it may affect the production environment. The ability to decrease the number of blocks is disabled by default as it may lead to ineffective usage of blocks when TPOSS is under load. You can change this behaviour by setting the **ippools.{range-name}.allow_to_decrease_number_of_blocks** to `true`, although it is not recommended.

You can change the IP pool block count, for example, to have new blocks activated more frequently and spread the IP usage better over the range. You can also increase the block count to avoid bottlenecks that occur due to heavy usage.

There are two ways to change the IP pool block count. The first one is to adjust the TPOSS cluster configuration followed by a restart. To change the block count, adjust the **ippools.{range-name}.blocks** configuration option. As a result, the next starting TPOSS instance will migrate the existing blocks to the desired size and count.

The second way is to adjust the number of blocks for the given IP pool at runtime without TPOSS restart. To do so, use the following command in the TPOSS remote console:

```elixir
> :rpc.multicall(Tposs.Utils, :put_conf, [[:pcs, :ippools, :{range-name}, :blocks], {block-count}])
{[:ok, :ok, ...], []}
> Tposs.Pcs.IpPools.run_cache()
[nil]
```

- `range-name` is an Elixir atom defined in the TPOSS values file at the path `ippools.{range-name}`.
- `block-count` is an integer with a static value set in the TPOSS values file at the path `ippools.{range-name}.blocks`.

> **NOTE:** If you change the number of blocks at runtime, adjust this IP pool configuration also in the `values.yaml` file. Otherwise, TPOSS will continue to use the original number of blocks after the restart.

## Service profiles configuration

To find a suitable IP pool, assign a DNS server, and a NAT pool, TPOSS must know what service profile to use for a given subscriber. Service profiles are provisioned by TPOSS during startup. You can specify them with the following configuration:

```yaml
profiles:
  # Homecountry name of the service profile
  DE:
    # Zone ID of the service profile
    zone_id: MOBILE
    # NAT pool name assigned to the service profile
    nat_pool_id: pool-de
    # List of IP pool names assigned to the service profile
    ippools: wan_ip_range
    # Primary DNS service assigned to the service profile
    primary_dns: 8.8.8.8
    # Secondary DNS service assigned to the service profile
    secondary_dns: 8.8.8.8
    # Service profile selector
    selector: "Called-Station-Id:apn.example.com"
    # Precedence of the service profile
    precedence: 0
    # Specifies if a service profile requires a provisioned subscriber in the database or not
    subscriber_required: true
```

>**NOTE:** The **DE** parameter specifies a home country name but it is recommended to switch to the new [configuration format](#multiple-profiles-with-same-home-country).

### Multiple profiles with same home country

In the previous paragraph, **DE** specifies a `homecountry` name for the given service profile. These configuration keys must be unique across all service profiles. If you need to specify multiple service profiles with the same `homecountry` name, provide the name of the `homecountry` in the **name** parameter. In this case, a selector is needed for TPOSS to know which profile to choose. That's why, specify the service profiles the same way as the **DE-1** and **DE-2** parameters in this example:

```yaml
profiles:
  # Unquie ID of the service profile
  DE-1:
    # Homecountry name of the service profile
    name: "DE"
    # Zone ID of the service profile
    zone_id: MOBILE
    # NAT pool name assigned to the service profile
    nat_pool_id: pool-de-1
    # List of IP pool names assigned to the service profile
    ippools: wan_ip_range-1
    # Service profile selector
    selector: "Called-Station-Id:apn1.example.com"
    # Precedence of the service profile
    precedence: 0
    # Specifies if a service profile requires a provisioned subscriber in the database or not
    subscriber_required: true
  # Unquie ID of the service profile
  DE-2:
    # Homecountry name of the service profile
    name: "DE"
    # Zone ID of the service profile
    zone_id: MOBILE
    # NAT pool name assigned to the service profile
    nat_pool_id: pool-de-2
    # List of IP pool names assigned to the service profile
    ippools: wan_ip_range-2
    # Service profile selector
    selector: "Called-Station-Id:apn2.example.com"
    # Precedence of the service profile
    precedence: 0
    # Specifies if a service profile requires a provisioned subscriber in the database or not
    subscriber_required: true
```

>**NOTE:** Since the **name** values for different profiles may be the same, the keys like **DE-1** and **DE-2** must be unique.

### Profiles selector

The service profile selector is an optional configuration parameter that is represented by the **profiles.{NAME}.selector** field. When selecting a service profile, TPOSS looks through all profiles and defines a set of profile candidates to choose from. The set of profile candidates contains:
- Profiles matching the selector
- Profiles with no selector defined 

The selector represents a set of key-value pairs split by a comma. The items of the set are interpreted as logical **AND** conditions. Each key must be split from its value with a colon `:`. The key represents the name of RADIUS AVP. The value is a regular expression that represents a value of this RADIUS AVP. See the example:

```yaml
# AVP-1 equals `a` AND AVP-2 may have any value that starts from `b`.
selector: "AVP-1:a,AVP-2:b.*"
```

### Profile selection

As described above, TPOSS must find a service profile to apply for a given subscriber at each RADIUS Access-Request. This process looks as follows:

1. TPOSS receives a RADIUS Access-Request from PGW.
2. TPOSS tries to fetch a subscriber and all the user data based on IMSI.
3. TPOSS checks all the provisioned service profiles and selects those for which the selector is matched or not configured.
4. TPOSS checks all the provisioned service profiles and selects those for which a home country name is matched with the subscriber's home country name (if the subscriber is provisioned and has the associated service profile) and **subscriber_required** is set to `true`.
5. If the list of profiles from step 4 is not empty, TPOSS looks through the list built in step 3 and tries to find a service profile with:
   - **subscriber_require** set to `true` 
   - home country name of the service profile matched with the homecountry name of the subscriber
6. If the list of profiles from step 5 is empty, the following steps depend on the subscriber:
   - If the subscriber is not provisioned, TPOSS looks for a service profile that is matched by the selector and **subscriber_required** is set to `false`.
   - If the subscriber is provisioned but does not have an associated service profile, TPOSS looks for a service profile that is matched by the selector and **subscriber_required** is set to `true`
7. If a service profile is not found, the request is rejected.

>**NOTE:** To prevent ambiguity during selection, do not specify two or more service profiles with the same selector, the same home country name, and the same values of **subscriber_required**. In such a case, TPOSS may not know which profile to choose. The behaviour in such cases is unpredictable and the first random service profile is used.

```mermaid
flowchart TD
  A[1. Receive RADIUS Access-Request] --> B[2. Find profiles that match the selector]-->C[3. Find profiles with the same homecountry name as subscriber has]-->D{3. Does a subscriber have a service profile\n that is matched with one of the configured profiles\n by selector and homecountry name\n and subscriber_required=true?}
  D -- yes --> E[4. Accept]
  D -- no  --> F{5. Is subscriber provisioned?}
  F -- yes --> F1[6. Find a service profile with the matched selector\n and subscriber_required=true or reject]
  F -- no --> F2[6. Find a service profile with the matched selector\n and subscriber_required=false or reject]
```

### Profiles without a subscriber

The user may not have a provisioned subscriber or associated service profile in the TPOSS database. You can specify service profiles that are not required to have subscribers for users with the **profiles.{NAME}.subscriber_required** configuration parameter. Such service profiles should not specify an IP pool, NAT IP pool, and DNS configuration.

### Profiles precedence

The **profiles.{NAME}.precedence** configuration parameter specifies the precedence of the service profile. If TPOSS finds two or more service profiles that can be applied for a user, it selects and applies a service profile with the highest precedence. You can change the precedence at runtime using the following API:

```elixir
Tposs.change_profile_precedence(profile_id, precedence)
```

- `profile_id` is the `{NAME}` of the service profile. The data type of `profile_id` is atom, so if the profile ID is `DE`, pass it as `:'DE'`.
- `precedence` is an integer number that represents the new value precedence for the given service profile.

>**NOTE:** Don't set **precedence** to value other than `0` by default. It should be used only in the maintenance mode. If you change this value at runtime, TPOSS does not preserve it after a restart and uses the original value instead. To preserve this value, adjust the `values.yaml` configuration file.

### IP pools

Each service profile may contain a set of IP pools separated by a comma or no IP pool at all. When there are more than one IP pool specified, the IP pool selector is used to find a proper pool. If there is no match, a random pool is used. If a service profile does not specify an IP pool range, TPOSS accepts such users anyway but an IP address is not allocated.
