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

# Service profiles and IP pools

TPOSS associates each user with a home country specified by a service profile. A service profile defines the following settings:

- NAT IP pool
- IP pools
- Primary and secondary DNS servers

This document describes how to configure service profiles and the related settings in TPOSS.

## Service profiles configuration

To find a suitable IP pool, assign a DNS server, and a NAT pool, TPOSS must know which service profile to use for a subscriber authorised in the network. TPOSS provisions service profiles during startup based on the provided configuration. See the example:

```yaml
profiles:
  # Home country name of the service profile
  DE:
    # An IP pool name assigned to the service profile
    ippools: wan_ip_range
    # NAT pool name assigned to the service profile
    nat_pool_id: pool-de
    # Primary DNS server assigned to the service profile
    primary_dns: 8.8.8.4
    # Secondary DNS server assigned to the service profile
    secondary_dns: 1.1.1.1
    # Service profile selector
    selector: "Called-Station-Id:apn.example.com"
    # Specifies if a service profile requires a provisioned subscriber
    subscriber_required: true
    # Precedence of the service profile
    precedence: 0
    # Zone ID of the service profile
    zone_id: MOBILE
    # Allows ignoring the profile name when selecting the profile
    ignore_profile_name: false
```

> [!NOTE]
> In this example, the **DE** parameter specifies a home country name, but it is recommended to switch to the new [configuration format](#multiple-service-profiles-with-same-home-country) for home country names.

### Multiple service profiles with same home country

In the previous section, **DE** specifies a home country name for the given service profile. This configuration key must be unique across all service profiles. If you need to specify multiple service profiles with the same home country name, use the **profiles.{PROFILE_ID}.name** parameter.

> [!NOTE]
> If you want to specify multiple service profiles with the same home country name, make sure that each such service profile has a configured [selector](#service-profiles-selector).

See the example for **profiles.DE-1** and **profiles.DE-2** parameters:

```yaml
profiles:
  # Unique ID of the service profile
  DE-1:
    # Home country name of the service profile
    name: "DE"
    # An IP pool name assigned to the service profile
    ippools: wan_ip_range-1
    # NAT pool name assigned to the service profile
    nat_pool_id: pool-de-1
    # Primary DNS server assigned to the service profile
    primary_dns: 8.8.8.4
    # Secondary DNS server assigned to the service profile
    secondary_dns: 1.1.1.1
    # Service profile selector
    selector: "Called-Station-Id:apn.de-1.example.com"
    # Specifies if a service profile requires a provisioned subscriber
    subscriber_required: true
    # Precedence of the service profile
    precedence: 0
    # Zone ID of the service profile
    zone_id: MOBILE
    # Allows ignoring the profile name when selecting the profile
    ignore_profile_name: false
  # Unique ID of the service profile
  DE-2:
    # Home country name of the service profile
    name: "DE"
    # An IP pool name assigned to the service profile
    ippools: wan_ip_range-2
    # NAT pool name assigned to the service profile
    nat_pool_id: pool-de-2
    # Primary DNS server assigned to the service profile
    primary_dns: 8.8.8.4
    # Secondary DNS server assigned to the service profile
    secondary_dns: 1.1.1.1
    # Service profile selector
    selector: "Called-Station-Id:apn.de-2.example.com"
    # Specifies if a service profile requires a provisioned subscriber
    subscriber_required: true
    # Precedence of the service profile
    precedence: 0
    # Zone ID of the service profile
    zone_id: MOBILE
    # Allows ignoring the profile name when selecting the profile
    ignore_profile_name: false
```

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

### Service profiles selector

The service profile selector is an optional configuration parameter that is represented by the **profiles.{PROFILE_ID}.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 but matched by name

The selector represents a set of key-value pairs split by a comma. Multiple 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
profiles:
  DE:
    # Value of Called-Station-Id AVP is `apn.example.com` and the value of NAS-Identifier starts from `apn1.nas`
    selector: "Called-Station-Id:apn.example.com,NAS-Identifier:apn1.nas.*"
```

If the selector is matched for the subscriber, but the home country name associated with the subscriber is not equal to the home country name associated with the profile, such profile is not selected. You can change this behaviour by setting the **profile.{PROFILE_ID}.ignore_profile_name** configuration option to `true`:

```yaml
profiles:
  DE:
    # Allows ignoring the profile name when selecting the profile
    ignore_profile_name: true
```

### Service profile selection

> [!NOTE]
> To prevent ambiguity during selection of a service profile, 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 random service profile is used.

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.
2. TPOSS tries to fetch a subscriber and all the user data based on the IMSI from the incoming RADIUS Access-Request.
3. TPOSS checks all the provisioned service profiles and selects those for which the selector is matched or selector not set at all.
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, and for which **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 that matches both name and selector, and has the highest precedence.
6. If the list of profiles from step 4 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 its **profiles.{PROFILE_ID}.subscriber_required** is set to `false`.
   - If the subscriber is provisioned, TPOSS looks for a service profile that matches the selector and has **profiles.{PROFILE_ID}.subscriber_required** set to `true`. Moreover, it must match the subscriber’s home country name or have **profiles.{PROFILE_ID}.ignore_profile_name** set to `true`.
7. If a service profile is not found, the request is rejected.

### Service 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 for such users with the **profiles.{PROFILE_ID}.subscriber_required** configuration parameter. Such service profiles must not specify these parameters:

- An IP pool
- A NAT IP pool
- Primary and secondary DNS servers

### Service profiles precedence

The **profiles.{PROFILE_ID}.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 `{PROFILE_ID}` 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 an IP pool. Such an IP pool must be defined in the [**ippools**](#ip-pools-configuration) section. If a service profile specifies an IP pool which is not configured in the **ippools** section, TPOSS prints an error on start and exits. If a service profile does not specify an IP pool range, TPOSS accepts such users anyway, but an IP address is not allocated.

## IP pools configuration

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

```yaml
# Timeout in milliseconds after which a released IP address can be re-used.
ip_recycling_timeout: 0

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.
    blocks: 20
    # Number of blocks to activate initially per instance.
    initial_active_blocks: 1
    # Percentage of block usage after which the block is marked as `inactive`.
    mark_inactive: 100
    # Percentage of busy IPs within the `inactive` block is marked as `active`.
    reactivate: 95
    # 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
    # in runtime.
    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.
- **initial_active_blocks** - the number of blocks to activate initially per instance. You can use this option to spread IP usage over the range.
- **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.
- **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.
- **packed** - the percentage of used IPs from a given block after which TPOSS tries to pre-fetch a new block of IP addresses.
- **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 its value 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.

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

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

```yaml
radius:
  # Allows creating 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 storing 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.{IP-POOL}.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.{IP-POOL}.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 change the number of blocks, use the following command in the TPOSS remote console:

```elixir
:rpc.multicall(Tposs.Utils, :put_conf, [[:pcs, :ippools, :{IP-POOL}, :blocks], {BLOCK-COUNT})
```

Then, apply the configuration at runtime:

```elixir
Tposs.Pcs.IpPools.run_cache()
```

- `IP-POOL` is an Elixir [atom](https://hexdocs.pm/elixir/1.12/Atom.html) defined in the TPOSS values file at the path `ippools.{IP-POOL}`.
- `BLOCK-COUNT` is an integer which represents number of blocks within an IP pool.

> [!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.
