> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/cgw/1.9/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/cgw/1.9/tutorials/01-imperative-ip-configuration.md).

# Imperative IP configuration

This tutorial explains how to configure IP-related entities, such as addresses, interfaces, or tunnels using a shell script.
This is the preferred way for setting up IP addresses, GRE tunnels, and VTI devices.

The initialisation script runs as the last init container, after VXLAN interfaces are attached to the pod.

## Add IP address to the interface

To start with a simple example, add IP addresses to an interface that already exists in the pod on startup.
This is an example of one simple configuration:

```yaml
initScript:
  enabled: true
  env:
    ADDITIONAL_IPV4_ADDRESS: 192.0.2.1
    ADDITIONAL_IPV4_CIDR: "24"

    ADDITIONAL_IPV6_ADDRESS: "2001:DB8::1"
    ADDITIONAL_IPV6_CIDR: "64"

    INTERFACE: eth0
  script: |
    set -xeu

    echo "set up additional IP address\n"
    ip addr add ${ADDITIONAL_IPV4_ADDRESS}/${ADDITIONAL_IPV4_CIDR} dev ${INTERFACE}
    ip -6 addr add ${ADDITIONAL_IPV6_ADDRESS}/${ADDITIONAL_IPV6_CIDR} dev ${INTERFACE}
```

In the second line, the module to run an initialisation script on startup is enabled.
Under the **initScript.env** parameter, you can set the environment variables. They can be used in the script itself.

It is highly recommended to make use of environment variables for configuration data, as it makes it is easier to copy the script itself for multiple deployments. If one variable is used multiple times, the actual value must be changed only in one place.

### Shell script

In the **initScript.script** parameter, provide a shell script as a long block string. The script must be a POSIX shell script, as `bash` is not supported.
You can use every program that is installed in the network tools container.

In this example, the first line sets the shell to output the commands that are executed (`set -x`). It also sets it to `check` if all environment variables are set, or to `fail` otherwise (`set -e`). Lastly, the parameter fails the whole script if one of the commands fails and does not continue with further settings (`set -u`).

After that, the actual shell script follows. In this case, a message is written to the output for debugging using the `echo` command.

In the second last line, the `ip` command is used to add the IPv4 address to the device. This is the standard command used on modern Linux distributions.
In the last line, add `-6` to most `ip` commands operating with IPv6 addresses.

## Add IP route to the interface

In the second example, add static routes to an existing interface:

```yaml
initScript:
  enabled: true
  env:
    ROUTE_PREFIX: 192.0.2.32/27
    ROUTER: 192.0.2.2

    ROUTE_IPV6_PREFIX: 2001:DB8:1::/48
    ROUTER_IPV6: 2001:DB8::2
  script: |
    set -xeu

    echo "set up additional route\n"
    ip route add ${ROUTE_PREFIX} via ${ROUTER}
    ip -6 route add ${ROUTER_IPV6_PREFIX} via ${ROUTER_IPV6}
```

In this example, the only difference is the last line in which `ip route` is used to add a route to the kernels routing table.
Like in the previous example, you must use `ip -6` to add routes to the IPv6 routing table.

## Change MTU of interfaces

If you have to change the MTU of interfaces manually, add the following parts to the init script:

```yaml
initScript:
  script: |
    ip link set dev eth0 mtu 1440
    # < add additional links>
```

## Disable offloading for NICs

To disable offloading of the network cards, add the following parts to the init script:

```yaml
initScript:
  script: |
    ethtool -K eth0 gro off
    ethtool -K eth0 tso off
    ethtool -K eth0 gso off
    # < add lines for additional interfaces >
```
