> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/smc/3.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/smc/3.15/config/aaa/diameter-concepts.md).

# Diameter Transport Configuration

## Overview

Diameter is an AAA protocol specified in [IETF RFC 6733][RFC6733]. It is the evolution of RADIUS with improved reliability, security, and extensibility. This document covers generic Diameter transport configuration that applies to all Diameter interfaces in SMC.

SMC uses Diameter for multiple interfaces:

- **Rf** - Offline charging (3GPP TS 32.299) - See [Rf Configuration](https://docs.cennso.com/components/smc/3.15/config/aaa/charging/rf.md)
- **Ro** - Online charging (3GPP TS 32.299) - See [Ro Configuration](https://docs.cennso.com/components/smc/3.15/config/aaa/charging/ro.md)
- **Gx** - Policy control (3GPP TS 29.212) - See [Gx Configuration](https://docs.cennso.com/components/smc/3.15/config/aaa/policy/gx.md)
- **NASREQ** - Network Access Server Requirements ([IETF RFC 7155][RFC7155]) - See [NASREQ Configuration](https://docs.cennso.com/components/smc/3.15/config/aaa/authentication/nasreq.md)

All of these interfaces share common Diameter transport infrastructure documented here.

References:
- [IETF RFC 6733][RFC6733] - Diameter Base Protocol
- [AAA Overview](https://docs.cennso.com/components/smc/3.15/config/aaa/overview.md) - Understanding the three AAA concerns

## Transport Configuration

### Common Diameter Settings

Diameter interfaces can share common transport settings or use separate per-interface configuration. Common settings are defined under `aaa.oss`:

```yaml
aaa:
  oss:
    interface: "bond0"
    ipv4:
      addr: "10.1.2.3"
    mtu: 1500
    destinations:
      - addr: "10.1.2.10"
        port: 3868
      - addr: "10.1.2.11"
        port: 3868
    destinationRealm: "example.com"
    originFqdn: "pgw.node1.example.com"
    originRealm: "node1.example.com"
```

Attributes:
- `interface` - Network interface name (references `networking.*` configuration)
- `ipv4.addr` - Source IPv4 address for Diameter connections
- `mtu` - Maximum Transmission Unit in bytes
- `destinations` - List of Diameter peer addresses and ports (load balanced)
- `destinationRealm` - Realm for Diameter routing
- `originFqdn` - Fully Qualified Domain Name identifying this node
- `originRealm` - Realm identifying this node

> [!NOTE]
> The `interface` and network parameters reference the `networking.*` configuration section. See [Network Configuration](https://docs.cennso.com/components/smc/3.15/config/networking.md) for details.

### Per-Interface Configuration

Each Diameter interface (Rf, Ro, Gx, NASREQ) can use the common settings or define its own. The `standalone` parameter controls this behaviour.

#### Using Common Settings

When `standalone` is `false` (or omitted), the interface uses settings from `aaa.oss`:

```yaml
aaa:
  oss:
    # Common settings for all interfaces
    destinations:
      - addr: "10.1.2.10"
        port: 3868
    destinationRealm: "example.com"
    originFqdn: "pgw.node1.example.com"
    originRealm: "node1.example.com"

  rf:
    enabled: true
    standalone: false  # Use common settings from aaa.oss
  
  gx:
    enabled: true
    standalone: false  # Also uses common settings
```

This is efficient when multiple interfaces connect to the same Diameter server (common in test environments).

#### Using Separate Settings

When `standalone` is `true`, the interface uses its own dedicated settings:

```yaml
aaa:
  ro:
    enabled: true
    standalone: true
    destinations:
      - addr: "10.2.3.5"
        port: 3868
      - addr: "10.2.3.6"
        port: 3868
    destinationRealm: "ocs.example.com"
    originFqdn: "pgw.node1.example.com"
    originRealm: "node1.example.com"
```

Use separate settings when different interfaces connect to different servers (common in production for separating charging from policy control).

#### Network Settings Association

Network settings (`interface`, `ipv4.addr`, `mtu`) are configured separately under the `networking` section:

```yaml
# Common network settings
networking:
  oss:
    interface: "bond0"
    ipv4:
      addr: "10.1.2.3"
    mtu: 1500

# Separate network settings for Ro
networking:
  ro:
    interface: "bond1"
    ipv4:
      addr: "10.5.6.7"
    mtu: 9000
```

**Association rule:**
- When `aaa.{interface}.standalone` is `false` → uses `networking.oss`
- When `aaa.{interface}.standalone` is `true` → uses `networking.{interface}`

See [Network Configuration](https://docs.cennso.com/components/smc/3.15/config/networking.md) for details on network interface configuration.

### Transport Protocol

Diameter supports TCP and SCTP (Stream Control Transmission Protocol) as transport protocols.

```yaml
aaa:
  rf:
    transportType: "tcp"  # or "sctp"
```

**TCP** is widely supported and suitable for most deployments. **SCTP** provides additional features:
- Multiple streams within a single connection
- Built-in multi-homing for resilience
- Message-oriented delivery
- Head-of-line blocking avoidance

> [!TIP]
> SCTP is recommended for high-throughput deployments with strict latency requirements. However, ensure network equipment and firewalls support SCTP before enabling.

**Default:** `tcp`

### Transport Socket Options

Configure transport layer behaviour with `transportOpts`:

```yaml
aaa:
  rf:
    transportType: "tcp"
    transportOpts:
      reuseaddr: true
      recbuf: 4096
      sndbuf: 4096
      nodelay: true
```

#### Socket Options

| Option | Type | Applies To | Description |
|---|---|---|---|
| reuseaddr | boolean | TCP, SCTP | Allow binding to an address in `TIME_WAIT` state |
| recbuf | integer | TCP, SCTP | Receive buffer size in kilobytes |
| sndbuf | integer | TCP, SCTP | Send buffer size in kilobytes |
| nodelay | boolean | TCP, SCTP | Sends packets immediately rather than batching them |
| unordered | boolean | SCTP | Allow unordered message delivery |
| netdev | string | TCP, SCTP | Bind the socket to a specific network device for VRF isolation |
| netns | string | TCP, SCTP | Open the socket in a specific Linux network namespace |

##### reuseaddr

When `true`, allows the socket to bind to an address that is in `TIME_WAIT` state from a previous connection. This enables faster restarts but may cause issues if old packets arrive.

**Default:** `false`

```yaml
transportOpts:
  reuseaddr: true
```

> [!TIP]
> Enable `reuseaddr` in development and test environments for faster restarts. Use with caution in production.

##### recbuf and sndbuf

Control the kernel socket buffer sizes. Larger buffers improve throughput but consume more memory.

**Default:** System defaults (typically 128 KB)

```yaml
transportOpts:
  recbuf: 2048  # 2 MB receive buffer
  sndbuf: 2048  # 2 MB send buffer
```

> [!NOTE]
> Buffer sizes are specified in kilobytes. The actual buffer may be doubled by the kernel.

**Tuning guidelines:**
- **Low latency** - Keep default or small buffers (128-512 KB)
- **High throughput** - Increase buffers (2-8 MB)
- **High latency links** - Increase buffers based on bandwidth-delay product

##### nodelay

When `true`, sends packets immediately rather than batching them. This disables Nagle's algorithm (TCP_NODELAY / SCTP_NODELAY socket options), which reduces message latency at the cost of slightly higher packet overhead.

**Impact:**
- `true` - Lower latency, higher overhead (more small packets)
- `false` - Higher latency, lower overhead (fewer larger packets)

**Recommendation:** Set to `true` for Diameter to reduce message latency.

```yaml
transportOpts:
  nodelay: true
```

Diameter messages are typically large enough (hundreds of bytes) that Nagle's algorithm provides minimal benefit, while adding latency.

##### unordered (SCTP only)

Allows SCTP to deliver messages out of order for better performance. Diameter applications must handle out-of-order messages correctly.

**Default:** `false`

```yaml
transportType: "sctp"
transportOpts:
  unordered: true
```

> [!CAUTION]
> When `unordered` is `true`, the application must not depend on message ordering within a stream. SMC's Diameter implementation handles out-of-order messages correctly, but external peers must also support this.

##### netdev

Binds the socket to a specific network device using `SO_BINDTODEVICE`. This enables
VRF-Lite isolation at the socket level: the operating system routes all traffic from this
socket through the specified device's routing table.

```yaml
transportOpts:
  netdev: "vrf-aaa"
```

Use `netdev` when Diameter traffic must be isolated from the default routing domain
without placing the entire interface into a Linux VRF. See
[Networking](https://docs.cennso.com/components/smc/3.15/config/networking.md#additional-interfaces-for-radius-andor-diameter)
for background on AAA interface routing constraints.

##### netns

Opens the socket in a specific Linux network namespace. Provide the full path to the
namespace file.

```yaml
transportOpts:
  netns: "/var/run/netns/aaa"
```

> [!NOTE]
> The target network namespace must exist before SMC starts. SMC does not create network namespaces.

## Connection Management

### Peer Selection

SMC maintains connections to multiple Diameter peers and selects the best peer for each request based on:
1. **Connection status** - Only `up` connections are considered
2. **Outstanding requests** - Prefer peers with fewer outstanding requests
3. **Rate limit tokens** - Prefer peers with available rate limit tokens

The peer with the lowest load is selected for each request.

**Load balancing algorithm:**
```
peer_score = outstanding_requests + (rate_limit_exhausted ? 1000 : 0)
selected_peer = min(peer_score for peer in available_peers)
```

This provides simple round-robin behaviour under normal conditions, with automatic failover to healthy peers when issues arise.

### Multiple Peers Configuration

Configure multiple peers for load balancing and redundancy:

```yaml
aaa:
  oss:
    destinations:
      - addr: "10.1.2.10"
        port: 3868
      - addr: "10.1.2.11"
        port: 3868
      - addr: "10.1.2.12"
        port: 3868
    destinationRealm: "example.com"
    originFqdn: "pgw.node1.example.com"
    originRealm: "node1.example.com"
```

**Behaviour:**
- Connections to all peers are established at startup
- Failed connections are retried automatically
- Requests are distributed across healthy peers
- No manual failover required

### Connection Monitoring

Monitor Diameter connections using the `diameter_connections` metric. This metric shows the number of active connections per Diameter service.

**Example Prometheus query:**

```promql
diameter_connections{svc="ergw_rf"}
```

Labels:
- `svc` - Diameter service name (`ergw_rf`, `ergw_ro`, `ergw_gx`, `ergw_nasreq`)

**Expected values:**
- `0` - No connections (configuration issue or all peers down)
- `N` - Number of configured peers (healthy state)
- `< N` - Some peers down (partial connectivity)

### Connection Failures

When a connection to a Diameter peer fails, SMC automatically attempts to reconnect. Messages are queued or redirected during the reconnection period.

**Failure handling per interface:**

| Interface | Behaviour During Outage |
|---|---|
| **Rf (Offline Charging)** | Messages are queued in memory until peers recover |
| **Ro (Online Charging)** | Sessions may be allowed with OCS Free (see [Ro Configuration](https://docs.cennso.com/components/smc/3.15/config/aaa/charging/ro.md)) |
| **Gx (Policy Control)** | Sessions proceed with default (statically configured) policies |
| **NASREQ** | Authentication/accounting requests fail, sessions may be rejected |

> [!CAUTION]
> During Rf connection failures, offline charging data is stored in SMC memory. Monitor memory consumption carefully during extended outages.

**Automatic reconnection:**
- Retry interval starts at 1 second
- Increases exponentially up to 60 seconds
- Continues indefinitely until connection succeeds
- No manual intervention required

## Rate Limiting

### Per-Peer Rate Limits

Rate limiting prevents overloading Diameter peers. Limits are configured on a per-peer basis using a token bucket algorithm.

```yaml
diameter:
  rateLimits:
    peers:
      - addr: "10.1.2.10"
        port: 3868
        rate: 1000                    # Requests per second
        outstanding_requests: 5000    # Maximum outstanding requests
      - addr: "10.1.2.11"
        port: 3868
        rate: 1500
        outstanding_requests: 5000
```

#### rate

Maximum number of requests per second to the peer. The token bucket refills at a constant rate.

**Token refill interval:**
```
Token refill interval = 1000 / rate milliseconds per token
```

**Example:** With `rate: 1000`, one token is added every 1 ms.

When the rate limit is reached, requests are automatically:
1. Sent to a different peer (if available)
2. Queued (if no peers available)
3. Dropped (if queue is full)

#### outstanding_requests

Maximum number of requests sent but not yet answered. When this limit is reached, new requests are queued or sent to a different peer.

**Purpose:** Prevents memory exhaustion from pending requests.

**Tuning:**
- **Low value (1000-2000)** - Tight control, may limit throughput
- **Medium value (5000-10000)** - Balanced performance
- **High value (20000+)** - Maximum throughput, higher memory usage

### Token Bucket Algorithm

Each peer has a token bucket with capacity equal to the `rate`. Tokens are added at a constant rate:

```
Token refill interval = 1000 / rate milliseconds
Bucket capacity = rate tokens
```

Each request consumes one token. When tokens are exhausted, requests wait for tokens to become available (if queued) or are sent to a different peer.

**Algorithm:**
1. Check if tokens are available in bucket
2. If yes, consume token and send request
3. If no, try a different peer
4. If no peers have tokens, queue or drop request

### Request Retry Across Peers

When a request cannot be sent to a peer (due to rate limiting, connection failure, or timeout), SMC retries the request to a different peer.

**Retry conditions:**
- Connection to the initial peer is down
- Request times out (fixed timeout: 5000 milliseconds)
- Rate limit reached for the initial peer

**Retry behaviour:**
- Only one retry attempt per peer
- Retransmitted messages keep the original Hop-by-Hop Identifier
- Retransmit flag (`T` bit) is set in retransmitted messages
- Peers already tried for this request are excluded from retry

> [!NOTE]
> The fixed timeout of 5000 milliseconds is not configurable.

### Monitoring Rate Limits

Rate limit behaviour is not directly exposed via Prometheus metrics. Monitor indirectly through:

**Request latency (increases when rate limited):**
```promql
histogram_quantile(0.95, 
  rate(diameter_request_duration_seconds_bucket[5m]))
```

**Connection load distribution (uneven may indicate rate limiting):**
```promql
diameter_outstanding_requests{svc="ergw_rf"}
```

If one peer consistently has more outstanding requests than others, it may indicate:
- Rate limiting on other peers
- Connection issues with other peers
- Uneven peer capacity

## Example Configurations

### Basic TCP Configuration

Simple configuration with single peer:

```yaml
aaa:
  oss:
    destinations:
      - addr: "192.168.1.20"
        port: 3868
    destinationRealm: "example.com"
    originFqdn: "pgw1.example.com"
    originRealm: "example.com"
  
  rf:
    enabled: true
    standalone: false
    transportType: "tcp"
    transportOpts:
      reuseaddr: true
      recbuf: 1024
      sndbuf: 1024
      nodelay: true
```

### SCTP with Separate Network

Ro interface using SCTP on a dedicated network interface:

```yaml
networking:
  ro:
    interface: "bond1"
    ipv4:
      addr: "10.5.6.7"
    mtu: 9000

aaa:
  ro:
    enabled: true
    standalone: true
    destinations:
      - addr: "10.5.6.20"
        port: 3868
      - addr: "10.5.6.21"
        port: 3868
    destinationRealm: "ocs.example.com"
    originFqdn: "pgw1.node.example.com"
    originRealm: "node.example.com"
    transportType: "sctp"
    transportOpts:
      reuseaddr: true
      recbuf: 4096
      sndbuf: 4096
      unordered: true
```

### High Availability with Rate Limiting

Multiple peers with rate limiting for high availability and performance:

```yaml
aaa:
  oss:
    destinations:
      - addr: "192.168.1.20"
        port: 3868
      - addr: "192.168.1.21"
        port: 3868
      - addr: "192.168.1.22"
        port: 3868
    destinationRealm: "example.com"
    originFqdn: "pgw1.example.com"
    originRealm: "example.com"
  
  rf:
    enabled: true
    standalone: false
    transportType: "tcp"
    transportOpts:
      reuseaddr: true
      recbuf: 2048
      sndbuf: 2048
      nodelay: true

diameter:
  rateLimits:
    peers:
      - addr: "192.168.1.20"
        port: 3868
        rate: 2000
        outstanding_requests: 10000
      - addr: "192.168.1.21"
        port: 3868
        rate: 2000
        outstanding_requests: 10000
      - addr: "192.168.1.22"
        port: 3868
        rate: 2000
        outstanding_requests: 10000
```

Total capacity: 6000 requests/second with automatic failover.

### Mixed Interface Configuration

Different interfaces connecting to different servers:

```yaml
aaa:
  # Charging uses common OSS/BSS server
  oss:
    destinations:
      - addr: "10.1.1.10"
        port: 3868
    destinationRealm: "charging.example.com"
    originFqdn: "pgw1.example.com"
    originRealm: "example.com"
  
  rf:
    enabled: true
    standalone: false  # Uses aaa.oss
  
  # Policy uses dedicated PCRF
  gx:
    enabled: true
    standalone: true
    destinations:
      - addr: "10.2.2.10"
        port: 3868
      - addr: "10.2.2.11"
        port: 3868
    destinationRealm: "pcrf.example.com"
    originFqdn: "pgw1.example.com"
    originRealm: "example.com"
  
  # Online charging uses dedicated OCS
  ro:
    enabled: true
    standalone: true
    destinations:
      - addr: "10.3.3.10"
        port: 3868
    destinationRealm: "ocs.example.com"
    originFqdn: "pgw1.example.com"
    originRealm: "example.com"
```

## Troubleshooting

### No Diameter Connections

**Symptom:** Metric `diameter_connections` shows 0 connections.

**Possible causes:**
- Network connectivity issues
- Firewall blocking TCP/SCTP port 3868
- Incorrect peer addresses in `destinations`
- Diameter realm mismatch
- Certificate issues (if TLS enabled)

**Solution:**

1. Verify network connectivity:
   ```bash
   ping {PEER_ADDRESS}
   ```

2. Check port connectivity:
   ```bash
   # For TCP
   telnet {PEER_ADDRESS} 3868
   
   # For SCTP
   nc -v {PEER_ADDRESS} 3868
   ```

3. Review SMC logs for Diameter connection errors:
   ```bash
   kubectl logs {POD_NAME} | grep -i "diameter.*connect"
   ```

4. Verify `destinationRealm` matches the peer's realm configuration

5. Check that `originFqdn` and `originRealm` are correctly configured and match peer expectations

### High Latency

**Symptom:** Diameter request latency increases significantly.

**Possible causes:**
- Rate limiting active (peers overloaded)
- Small socket buffers
- Nagle's algorithm enabled (`nodelay: false`)
- Network congestion
- Peer processing delays

**Solution:**

1. Check peer load distribution (all peers should have similar load):
   ```promql
   diameter_outstanding_requests{svc="ergw_rf"}
   ```

2. Increase socket buffers:
   ```yaml
   transportOpts:
     recbuf: 4096
     sndbuf: 4096
   ```

3. Enable `nodelay`:
   ```yaml
   transportOpts:
     nodelay: true
   ```

4. Increase rate limits if peers can handle more load:
   ```yaml
   diameter:
     rateLimits:
       peers:
         - addr: "{PEER_ADDRESS}"
           rate: 3000  # Increase from 2000
           outstanding_requests: 15000  # Increase from 10000
   ```

5. Add more peers for better load distribution

### Memory Growth During Rf Outage

**Symptom:** SMC memory usage increases significantly when Rf connection is down.

**Cause:** Offline charging data is queued in memory during Rf outages.

**Solution:**

1. Restore Rf connectivity as soon as possible

2. Monitor memory usage:
   ```promql
   container_memory_usage_bytes{pod=~"smc-.*"}
   ```

3. If memory continues to grow towards the limit:
   - Increase memory limits temporarily
   - Prioritise Rf connection restoration
   - As last resort, restart SMC (loses queued charging data)

> [!IMPORTANT]
> Restarting SMC during an Rf outage results in loss of offline charging records that were queued in memory.

### Uneven Load Distribution

**Symptom:** One peer receives significantly more requests than others.

**Possible causes:**
- Other peers experiencing connection issues
- Different rate limits configured per peer
- One peer responding faster than others

**Solution:**

1. Check connection status for all peers:
   ```promql
   diameter_connections{svc="ergw_rf"}
   ```

2. Verify rate limits are consistent:
   ```yaml
   diameter:
     rateLimits:
       peers:
         - addr: "10.1.2.10"
           rate: 2000  # Should match
         - addr: "10.1.2.11"
           rate: 2000  # Should match
   ```

3. Check peer response times (slow peers will accumulate more outstanding requests and receive less load)

4. If intentional (peers have different capacity), configure appropriate rate limits per peer

## Related Documentation

- [AAA Overview](https://docs.cennso.com/components/smc/3.15/config/aaa/overview.md) - Understanding the three AAA concerns
- [Network Configuration](https://docs.cennso.com/components/smc/3.15/config/networking.md) - Network interface configuration
- [Rf Offline Charging](https://docs.cennso.com/components/smc/3.15/config/aaa/charging/rf.md) - Rf-specific configuration
- [Ro Online Charging](https://docs.cennso.com/components/smc/3.15/config/aaa/charging/ro.md) - Ro-specific configuration
- [Gx Policy Control](https://docs.cennso.com/components/smc/3.15/config/aaa/policy/gx.md) - Gx-specific configuration
- [Diameter NASREQ](https://docs.cennso.com/components/smc/3.15/config/aaa/authentication/nasreq.md) - NASREQ-specific configuration

[RFC6733]: https://www.rfc-editor.org/rfc/rfc6733.html
[RFC7155]: https://www.rfc-editor.org/rfc/rfc7155.html
