> 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/config/charging.md).

# Charging

## AAA Transports
Since all mentioned applications are delivered on Diameter interfaces, their configuration shares those properties that apply to the Diameter protocol. There is a configuration element that holds common Diameter properties that all of these applications can share if necessary.

The first step when provisioning charging is to configure the network used for the different protocols, Rf and Ro. Ro can use a common interface, IPv4 address and MTU, or use specific per-protocol settings. The common settings are `networking.oss.{interface,ipv4.addr,mtu}`, whereas Ro uses `networking.ro.{interface,ipv4.addr,mtu}`.

Now the specific charging protocol settings can be configured. They are found under `aaa.rf` and `aaa.ro`. For both charging protocols, the following settings can be configured:

* `enabled`: whether signalling will be sent on this interface
* `standalone`: whether the common settings for OSS will be used, or rather the separate settings will be used

A value of `standalone: false` will mean the protocol will use the values under `aaa.oss` for `destinations`,`destinationRealm`, `originFqdn` and `originRealm`. Otherwise the values under `aaa.rf` or `aaa.ro` will be used.

Then the specific transport protocol (TCP or SCTP) and socket options options need to be configured, both under `aaa.rf` or `aaa.ro`:

* `transportType`: either `sctp` or `tcp`
* `transportOpts`: specific
  * `reuseaddr`: whether an existing socket in `TIME_WAIT` state is ignored when setting up the socket
  * `recbuf`: the buffer for incoming packets, in KB
  * `sndbuf`: the buffer for ougoing packets, in KB
  * `unordered`: *(only usable for SCTP)* whether messages can be sent unordered in the SCTP socket. Its recommended value is `true`.
  * `nodelay`: disables [Nagle's algorithm](https://en.wikipedia.org/wiki/Nagle%27s_algorithm); this may improve overhead in connections, but may cause latency for specific data to increase as they are not sent immediately

### Configure sender identity in the `Session-Id` AVP

In the Diameter interfaces (Gx, Gy, NASREQ), the `Session-Id` AVP starts with the sender's identity. You can configure this identity through the `aaa.originHost` parameter. If not set, the SMC identity is retrieved from the operating system.

You can also modify sender identity directly in the `ergw-config.json` file under the entry `"aaa"."apps"[0]."Origin-Host": "some.fqdn.name"`.

## Retry a request across peers

The SMC Diameter client sends Diameter requests to one peer from those listed under the `diameter.rateLimits.peers` property. SMC selects the peer with the lowest load based on outstanding requests and request tokens. The SMC Diameter client retries a request to a different peer if:
- Connection to the initial peer is down
- Request times out after a fixed timeout of 5000ms
- Request is dropped because the rate limit for that peer is reached (see the [Diameter rate control](#diameter-rate-control) section below for more details)

Only a single request attempt is performed for each peer.

The retransmitted request messages are sent to a peer that wasn't used yet. These messages keep the ID of the original request and have the retransmit flag set. 

## OCS Free

OCS Free is a feature that allows user sessions to continue with a predefined granted time or volume quota in case the Online Charging System (OCS) is unavailable or OCS requests fail due to temporary issues, such as transport interruptions. You can enable this feature on the Ro interface through the `aaa.ro.ocsFree.enabled` property.

If this feature is enabled, a predefined AAA response is used for the user data session in the following cases:

* OCS is down
* Request times out
* Rate limit is exceeded

For every `{RULE_NAME}` item under the `aaa.applicationDetection.rules` property, one entry is added to the `Multiple-Services-Credit-Control` AVP response. See the example:

```yaml
Multiple-Services-Credit-Control:
# If `aaa.applicationDetection.rules.{RULE_NAME_1}.onlineEnabled` is set to `true`, SMC returns such a response:
- Envelope-Reporting: 0
  Granted-Service-Unit:
    CC-Time: <random value between aaa.ro.ocsFree.ccTimeMin and aaa.ro.ocsFree.ccTimeMax>
    Rating-Group: <aaa.applicationDetection.rules.{RULE_NAME_1}.onlineRatingGroup>
    Result-Code: 2001,
    Time-Quota-Threshold: 60
- ...
# If `aaa.applicationDetection.rules.{RULE_NAME_N}.onlineEnabled` is set to `true`, SMC returns such a response:
- Envelope-Reporting: 0
  Granted-Service-Unit:
    CC-Time: <random value between aaa.ro.ocsFree.ccTimeMin and aaa.ro.ocsFree.ccTimeMax>
    Rating-Group: <aaa.applicationDetection.rules.{RULE_NAME_N}.onlineRatingGroup>
    Result-Code: 2001,
    Time-Quota-Threshold: 60
Result-Code: 2001
```

When this predefined granted quota is exhausted, the user data session is stopped without any communication with the OCS.

## Diameter rate control

To avoid overloading a Diameter peer, you can apply a rate limit on a per-peer basis. Requests exceeding the rate are dropped and may be redirected to a different peer.

The rate limit uses a token bucket algorithm. You can configure a request rate per second through the `diameter.rateLimits.peers.[].rate` property. The token refill interval is 1000/*rate* milliseconds. When the rate limit is reached, the outgoing request rate is constant. You can configure a per-peer limit for outstanding (not yet answered) requests through the `diameter.rateLimits.peers.[].outstanding_requests` property.

## RADIUS charging

The `aaa.radius.*` parameters specify how RADIUS charging will be performed. RADIUS needs to enabled with `aaa.radius.enabled` set to `true`. The different phases of RADIUS accounting can be separately enabled with the `aaa.radius.start.enabled`, `aaa.radius.interim.enabled` and `aaa.radius.stop.enabled` parameters. Additionally, specific AVPs can be filtered out using `aaa.radius.avp_filters` if needed.

Sample configuration:

```yaml
aaa:
  radius:
    enabled: true
    start:
      enabled: true
    stop:
      enabled: true
    server:
      addr: 172.22.14.1
      port: 1812
      type: IPv4 # ipv4` or `ipv6` or `fqdn`
      password: secret-tposs
```

Separate servers for authorization and accounting can be used by setting `aaa.radius.server.acct.standalone` to `true`, and using `aaa.radius.server.{acct,auth}.addr` and `aaa.radius.server.{acct,auth}.type`:

Example:

```yaml
aaa:
  radius:
    enabled: true
    start:
      enabled: true
    stop:
      enabled: true
    server:
      acct:
        standalone: true
        addr: 1.2.3.4
        port: 1812
        type: IPv4 # ipv4` or `ipv6` or `fqdn`
        password: secret-tposs
      auth:
        addr: 5.6.7.8
        port: 1812
        type: IPv4 # ipv4` or `ipv6` or `fqdn`
        password: secret-tposs
```

### RADIUS Accounting without Authentication

To enable RADIUS with only Accounting, without Authentication, and use `start`, `stop` and `interim`, use following configuration:

```yaml
aaa:
  acctInterimInterval: 60 # seconds
  radius:
    enabled: true
    authenticate:
      enabled: false
    interim:
      enabled: true
    start:
      enabled: true
    stop:
      enabled: true
    server:
      addr: 172.22.14.1
      port: 1812
      type: IPv4 # ipv4` or `ipv6` or `fqdn`
      password: secret-tposs
```

## NASREQ settings

By default, interim accounting is enabled and NASREQ requests are sent at the default rate of 1 request per 1800 seconds. This creates additional traffic on the PFCP interface. To disable this option, it is possible to set the `aaa.acctInterimInterval` to `null` as in the following example:

```yaml
aaa:
  acctInterimInterval: null
```
>**NOTE** A `null` value cannot be used for `aaa.acctInterimInterval` if RADIUS stop or interim accounting are enabled.

## Charging parameters

The triggers enabled for charging in the SMC vary depending on its role:

* SMC as SMF:
  * charging containers updated at `cgi-sai-change`, `ecgi-change`, `qos-change`, `rai-change`, `tai-change`, `tariff-switch-change` and `user-location-info-change`
  * CDRs sent at `ms-time-zone-change`, `rat-change`, `sgsn-sgw-change` and `sgsn-sgw-plmn-id-change`
* SMC as TDF:
  * charging containers updated at `sgsn-sgw-plmn-id-change` and `tariff-switch-change`
  * CDRs sent at `ms-time-zone-change`

The location-dependent triggers (`cgi-sai-change`, `ecgi-change`, `rai-change`, `tai-change`, and `user-location-info-change`) enable sending a Change Reporting Action (CRA) IE in the Create Session Response messages sent through the GTPv2 interface. Whether the CRA IE is sent and what value is included in it depend on the values of the CRSI and ENBCRSI indications in the `Indication Flags` IE, the access network type in the `RAT Type` IE, and the configured triggers. These IEs are taken from the original Create Session Request message. The following table describes the CRA IE value sent depending on different combinations of the indications and triggers:

| Access network type | CRSI received | ENBCRSI received | Active triggers | CRA IE value |
| :-: | :-: | :-: | :-: | --- |
| any | `false` | any | any | No CRA sent |
| EUTRAN | `true` | `true` | `tai-change` AND `user-location-info-change` | Start Reporting TAI, Macro eNodeB ID and Extended Macro eNodeB ID: `8` |
| EUTRAN | `true` | `true` | `user-location-info-change` | Start Reporting Macro eNodeB ID and Extended Macro eNodeB ID: `7` |
| EUTRAN | `true` | any | `tai-change` AND `ecgi-change` | Start Reporting TAI and ECGI: `6` |
| EUTRAN | `true` | any | `tai-change` | Start reporting TAI: `3` |
| EUTRAN | `true` | any | `ecgi-change` | Start Reporting ECGI: `4` |
| EUTRAN | `true` | any | `cgi-sai-change` OR `rai-change` | No CRA sent |
| UTRAN | `true` | any | `user-location-info-change` | Start Reporting CGI/SAI and RAI: `5` |
| UTRAN | `true` | any | `cgi-sai-change` AND `rai-change` | Start Reporting CGI/SAI and RAI: `5` |
| UTRAN | `true` | any | `cgi-sai-change` | Start Reporting CGI/SAI: `1` |
| UTRAN | `true` | any | `rai-change` | Start Reporting RAI: `2` |
| UTRAN | `true` | any | `tai-change` OR `ecgi-change` | No CRA sent |

More information about charging triggers can be found in [TS 3GPP 32.251](https://portal.3gpp.org/desktopmodules/Specifications/SpecificationDetails.aspx?specificationId=1898), sections 5.2.3.4 "Triggers for PGW-CDR charging information collection", 5.2.3.9 "Triggers for TDF-CDR charging information collection" and 5.2.3.10 "Triggers for PGW-CDR charging information collection when IP-CAN session charging is active".

Each `rules` definition under the `applicationDetection` section can include the corresponding policy items; any reference to AVP definitions can be checked in TS 3GPP 29.212, section 5.3 "Gx specific AVPs" (except as noted):

- `precedence` <br>
  The order in which this rule will be applied if the flow definitions in several rules match the same flow. Corresponds to the `Precedence` AVP.

- `permittedIPs` <br>
  Sets the IPv4 addresses for which flows with the specification `permit out ipv4 from <address> to assigned` will be added.

- `type` <br>
  Whether flow descriptions (`classic`) or an application name (`adf`) will be used to identify flows.

- `additionalSDF` (only applicable to `classic` type) <br>
  The flow specification corresponding to the `Flow-Description` AVP. The format for the flow description is described in RFC6733 (`IPFilterRule`). It is mapped to the `PDI.SDF_Filter.Flow_Description` field in PFCP. Both `UPLINK` and `DOWNLINK` directions are set up.

- `adf` type <br>
  The name of the rule is used as the value for the `TDF-Application-Identifier` AVP.

- `meteringMethod` <br>
  Describes what will be measured and reported in the flow specified in the element above. Each integer represents a different concept. In the packet switching domain, volume reporting is the usual setting.

    | Concept | Value |
    | --- | ---|
    | DURATION | 0 |
    | VOLUME | 1 |
    | DURATION_VOLUME | 2 |
    | EVENT | 3 |

- `offlineEnabled` <br>
  Set to 1 if offline charging is enabled.

- `onlineEnabled` <br>
  Set to 1 if online charging is enabled.

- `ratingGroup` <br>
  Rating Group applied to the flow. A description on the grouping of service usage can be found in RFC4006, section 5.1.2.

- `offlineRatingGroup` <br>
  The Rating Group defined for offline charging. Overrides the general `ratingGroup` setting.

- `onlineRatingGroup` <br>
  The Rating Group defined for offline charging. Overrides the general `ratingGroup` setting.

- `Redirect-Information`
  <TBD>

When several rules are sent to the UPG, the rule to apply depends on a combination of flow specification and precedence, so the UPG will activate the highest-precedence rule that applies to the current flow. A rulebase selects several such rules to be sent to the UPG.
