github.com/baraj55/containernetworking-cni@v0.7.2-0.20200219164625-56ace59a9e7f/SPEC.md (about)

     1  # Container Network Interface Specification
     2  
     3  - [Version](#version)
     4  - [Overview](#overview)
     5  - [General considerations](#general-considerations)
     6  - [CNI Plugin](#cni-plugin)
     7    * [Overview](#overview-1)
     8    * [Parameters](#parameters)
     9    * [Result](#result)
    10    * [Network Configuration](#network-configuration)
    11    * [Example configurations](#example-configurations)
    12    * [Network Configuration Lists](#network-configuration-lists)
    13    * [IP Allocation](#ip-allocation)
    14    * [Well-known Structures](#well-known-structures)
    15  - [Well-known Error Codes](#well-known-error-codes)
    16  
    17  ## Version
    18  
    19  This is CNI **spec** version **0.4.0**.
    20  
    21  Note that this is **independent from the version of the CNI library and plugins** in this repository (e.g. the versions of [releases](https://github.com/containernetworking/cni/releases)).
    22  
    23  #### Released versions
    24  
    25  Released versions of the spec are available as Git tags.
    26  
    27  | tag                                                                                  | spec permalink                                                                        | major changes                     |
    28  | ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------- | --------------------------------- |
    29  | [`spec-v0.4.0`](https://github.com/containernetworking/cni/releases/tag/spec-v0.4.0) | [spec at v0.4.0](https://github.com/containernetworking/cni/blob/spec-v0.4.0/SPEC.md) | Introduce the CHECK command and passing prevResult on DEL |
    30  | [`spec-v0.3.1`](https://github.com/containernetworking/cni/releases/tag/spec-v0.3.1) | [spec at v0.3.1](https://github.com/containernetworking/cni/blob/spec-v0.3.1/SPEC.md) | none (typo fix only)              |
    31  | [`spec-v0.3.0`](https://github.com/containernetworking/cni/releases/tag/spec-v0.3.0) | [spec at v0.3.0](https://github.com/containernetworking/cni/blob/spec-v0.3.0/SPEC.md) | rich result type, plugin chaining |
    32  | [`spec-v0.2.0`](https://github.com/containernetworking/cni/releases/tag/spec-v0.2.0) | [spec at v0.2.0](https://github.com/containernetworking/cni/blob/spec-v0.2.0/SPEC.md) | VERSION command                   |
    33  | [`spec-v0.1.0`](https://github.com/containernetworking/cni/releases/tag/spec-v0.1.0) | [spec at v0.1.0](https://github.com/containernetworking/cni/blob/spec-v0.1.0/SPEC.md) | initial version                   |
    34  
    35  *Do not rely on these tags being stable.  In the future, we may change our mind about which particular commit is the right marker for a given historical spec version.*
    36  
    37  
    38  ## Overview
    39  
    40  This document proposes a generic plugin-based networking solution for application containers on Linux, the _Container Networking Interface_, or _CNI_.
    41  It is derived from the rkt Networking Proposal, which aimed to satisfy many of the design considerations for networking in [rkt][rkt-github].
    42  
    43  For the purposes of this proposal, we define two terms very specifically:
    44  - _container_ can be considered synonymous with a [Linux _network namespace_][namespaces]. What unit this corresponds to depends on a particular container runtime implementation: for example, in implementations of the [App Container Spec][appc-github] like rkt, each _pod_ runs in a unique network namespace. In [Docker][docker], on the other hand, network namespaces generally exist for each separate Docker container.
    45  - _network_ refers to a group of entities that are uniquely addressable that can communicate amongst each other. This could be either an individual container (as specified above), a machine, or some other network device (e.g. a router). Containers can be conceptually _added to_ or _removed from_ one or more networks.
    46  
    47  This document aims to specify the interface between "runtimes" and "plugins". Whilst there are certain well known fields, runtimes may wish to pass additional information to plugins. These extensions are not part of this specification but are documented as [conventions](CONVENTIONS.md). The key words "must", "must not", "required", "shall", "shall not", "should", "should not", "recommended", "may" and "optional" are used as specified in [RFC 2119][rfc-2119].
    48  
    49  [rkt-github]: https://github.com/coreos/rkt
    50  [namespaces]: http://man7.org/linux/man-pages/man7/namespaces.7.html 
    51  [appc-github]: https://github.com/appc/spec
    52  [docker]: https://docker.com
    53  [rfc-2119]: https://www.ietf.org/rfc/rfc2119.txt
    54  
    55  ## General considerations
    56  
    57  - The container runtime must create a new network namespace for the container before invoking any plugins.
    58  - The runtime must then determine which networks this container should belong to, and for each network, which plugins must be executed.
    59  - The network configuration is in JSON format and can easily be stored in a file. The network configuration includes mandatory fields such as "name" and "type" as well as plugin (type) specific ones. The network configuration allows for fields to change values between invocations. For this purpose there is an optional field "args" which must contain the varying information.
    60  - The container runtime must add the container to each network by executing the corresponding plugins for each network sequentially.
    61  - Upon completion of the container lifecycle, the runtime must execute the plugins in reverse order (relative to the order in which they were executed to add the container) to disconnect the container from the networks.
    62  - The container runtime must not invoke parallel operations for the same container, but is allowed to invoke parallel operations for different containers.
    63  - The container runtime must order ADD and DEL operations for a container, such that ADD is always eventually followed by a corresponding DEL. DEL may be followed by additional DELs but plugins should handle multiple DELs permissively (i.e. plugin DEL should be idempotent).
    64  - A container must be uniquely identified by a ContainerID. Plugins that store state should do so using a primary key of `(network name, CNI_CONTAINERID, CNI_IFNAME)`.
    65  - A runtime must not call ADD twice (without a corresponding DEL) for the same `(network name, container id, name of the interface inside the container)`. This implies that a given container ID may be added to a specific network more than once only if each addition is done with a different interface name.
    66  - Fields in CNI structures (like [Network Configuration](#network-configuration) and [CNI Plugin Result](#result)) are required unless specifically marked optional.
    67  
    68  ## CNI Plugin
    69  
    70  ### Overview
    71  
    72  Each CNI plugin must be implemented as an executable that is invoked by the container management system (e.g. rkt or Kubernetes).
    73  
    74  A CNI plugin is responsible for inserting a network interface into the container network namespace (e.g. one end of a veth pair) and making any necessary changes on the host (e.g. attaching the other end of the veth into a bridge).
    75  It should then assign the IP to the interface and setup the routes consistent with the IP Address Management section by invoking appropriate IPAM plugin.
    76  
    77  ### Parameters
    78  
    79  The operations that CNI plugins must support are:
    80  
    81  - `ADD`: Add container to network
    82    - Parameters:
    83      - **Container ID**. A unique plaintext identifier for a container, allocated by the runtime. Must not be empty.  Must start with a alphanumeric character, optionally followed by any combination of one or more alphanumeric characters, underscore (_), dot (.) or hyphen (-).
    84      - **Network namespace path**. This represents the path to the network namespace to be added, i.e. /proc/[pid]/ns/net or a bind-mount/link to it.
    85      - **Network configuration**. This is a JSON document describing a network to which a container can be joined. The schema is described below.
    86      - **Extra arguments**. This provides an alternative mechanism to allow simple configuration of CNI plugins on a per-container basis.
    87      - **Name of the interface inside the container**. This is the name that should be assigned to the interface created inside the container (network namespace); consequently it must comply with the standard Linux restrictions on interface names, must not be empty, must not be "." or "..", must be less than 16 characters and must not contain / or : or any whitespace characters.
    88    - Result:
    89      - **Interfaces list**. Depending on the plugin, this can include the sandbox (eg, container or hypervisor) interface name and/or the host interface name, the hardware addresses of each interface, and details about the sandbox (if any) the interface is in.
    90      - **IP configuration assigned to each interface**. The IPv4 and/or IPv6 addresses, gateways, and routes assigned to sandbox and/or host interfaces.
    91      - **DNS information**. Dictionary that includes DNS information for nameservers, domain, search domains and options.
    92  
    93  - `DEL`: Delete container from network
    94    - Parameters:
    95      - **Container ID**, as defined above.
    96      - **Network namespace path**, as defined above.
    97      - **Network configuration**, as defined above.
    98      - **Extra arguments**, as defined above.
    99      - **Name of the interface inside the container**, as defined above.
   100    - All parameters should be the same as those passed to the corresponding add operation.
   101    - A delete operation should release all resources held by the supplied containerid in the configured network.
   102    - If there was a known previous `ADD` action for the container, the runtime MUST add a `prevResult` field to the configuration JSON of the plugin (or all plugins in a chain), which MUST be the `Result` of the immediately previous `ADD` action in JSON format ([see below](#network-configuration-list-runtime-examples)).  The runtime may wish to use libcni's support for caching `Result`s.
   103    - When `CNI_NETNS` and/or `prevResult` are not provided, the plugin should clean up as many resources as possible (e.g. releasing IPAM allocations) and return a successful response.
   104    - If the runtime cached the `Result` of a previous `ADD` response for a given container, it must delete that cached response on a successful `DEL` for that container.
   105  
   106  Plugins should generally complete a `DEL` action without error even if some resources are missing.  For example, an IPAM plugin should generally release an IP allocation and return success even if the container network namespace no longer exists, unless that network namespace is critical for IPAM management. While DHCP may usually send a 'release' message on the container network interface, since DHCP leases have a lifetime this release action would not be considered critical and no error should be returned. For another example, the `bridge` plugin should delegate the DEL action to the IPAM plugin and clean up its own resources (if present) even if the container network namespace and/or container network interface no longer exist.
   107  
   108  - `CHECK`: Check container's networking is as expected
   109    - Parameters:
   110      - **Container ID**, as defined for `ADD`.
   111      - **Network namespace path**, as defined for `ADD`.
   112      - **Network configuration** as defined for `ADD`, which must include a `prevResult` field containing the `Result` of the immediately preceding `ADD` for the container.
   113      - **Extra arguments**, as defined for `ADD`.
   114      - **Name of the interface inside the container**, as defined for `ADD`.
   115    - Result:
   116      - The plugin must return either nothing or an error.
   117    - The plugin must consult the `prevResult` to determine the expected interfaces and addresses.
   118    - The plugin must allow for a later chained plugin to have modified networking resources, e.g. routes.
   119    - The plugin should return an error if a resource included in the CNI Result type (interface, address or route) was created by the plugin, and is listed in `prevResult`, but is missing or in an invalid state.
   120    - The plugin should return an error if other resources not tracked in the Result type such as the following are missing or are in an invalid state:
   121      - Firewall rules
   122      - Traffic shaping controls
   123      - IP reservations
   124      - External dependencies such as a daemon required for connectivity
   125      - etc.
   126    - The plugin should return an error if it is aware of a condition where the container is generally unreachable.
   127    - The plugin must handle `CHECK` being called immediately after an `ADD`, and therefore should allow a reasonable convergence delay for any asynchronous resources.
   128    - The plugin should call `CHECK` on any delegated (e.g. IPAM) plugins and pass any errors on to its caller.
   129    - A runtime must not call `CHECK` for a container that has not been `ADD`ed, or has been `DEL`eted after its last `ADD`.
   130    - A runtime must not call `CHECK` if `disableCheck` is set to `true` in the [configuration list](#network-configuration-lists).
   131    - A runtime must include a `prevResult` field in the network configuration containing the `Result` of the immediately preceding `ADD` for the container. The runtime may wish to use libcni's support for caching `Result`s.
   132    - A runtime may choose to stop executing `CHECK` for a chain when a plugin returns an error.
   133    - A runtime may execute `CHECK` from immediately after a successful `ADD`, up until the container is `DEL`eted from the network.
   134    - A runtime may assume that a failed `CHECK` means the container is permanently in a misconfigured state.
   135  
   136  - `VERSION`: Report version
   137    - Parameters: NONE.
   138    - Result: information about the CNI spec versions supported by the plugin
   139  
   140        ```
   141        {
   142          "cniVersion": "0.4.0", // the version of the CNI spec in use for this output
   143          "supportedVersions": [ "0.1.0", "0.2.0", "0.3.0", "0.3.1", "0.4.0" ] // the list of CNI spec versions that this plugin supports
   144        }
   145        ```
   146  
   147  Runtimes must use the type of network (see [Network Configuration](#network-configuration) below) as the name of the executable to invoke.
   148  Runtimes should then look for this executable in a list of predefined directories (the list of directories is not prescribed by this specification). Once found, it must invoke the executable using the following environment variables for argument passing:
   149  - `CNI_COMMAND`: indicates the desired operation; `ADD`, `DEL`, `CHECK`, or `VERSION`.
   150  - `CNI_CONTAINERID`: Container ID
   151  - `CNI_NETNS`: Path to network namespace file
   152  - `CNI_IFNAME`: Interface name to set up; if the plugin is unable to use this interface name it must return an error
   153  - `CNI_ARGS`: Extra arguments passed in by the user at invocation time. Alphanumeric key-value pairs separated by semicolons; for example, "FOO=BAR;ABC=123"
   154  - `CNI_PATH`: List of paths to search for CNI plugin executables. Paths are separated by an OS-specific list separator; for example ':' on Linux and ';' on Windows
   155  
   156  Network configuration in JSON format must be streamed to the plugin through stdin. This means it is not tied to a particular file on disk and may contain information which changes between invocations.
   157  
   158  
   159  ### Result
   160  
   161  Note that IPAM plugins should return an abbreviated `Result` structure as described in [IP Allocation](#ip-allocation).
   162  
   163  Plugins must indicate success with a return code of zero and the following JSON printed to stdout in the case of the ADD command. The `ips` and `dns` items should be the same output as was returned by the IPAM plugin (see [IP Allocation](#ip-allocation) for details) except that the plugin should fill in the `interface` indexes appropriately, which are missing from IPAM plugin output since IPAM plugins should be unaware of interfaces.
   164  
   165  ```
   166  {
   167    "cniVersion": "0.4.0",
   168    "interfaces": [                                            (this key omitted by IPAM plugins)
   169        {
   170            "name": "<name>",
   171            "mac": "<MAC address>",                            (required if L2 addresses are meaningful)
   172            "sandbox": "<netns path or hypervisor identifier>" (required for container/hypervisor interfaces, empty/omitted for host interfaces)
   173        }
   174    ],
   175    "ips": [
   176        {
   177            "version": "<4-or-6>",
   178            "address": "<ip-and-prefix-in-CIDR>",
   179            "gateway": "<ip-address-of-the-gateway>",          (optional)
   180            "interface": <numeric index into 'interfaces' list>
   181        },
   182        ...
   183    ],
   184    "routes": [                                                (optional)
   185        {
   186            "dst": "<ip-and-prefix-in-cidr>",
   187            "gw": "<ip-of-next-hop>"                           (optional)
   188        },
   189        ...
   190    ],
   191    "dns": {                                                   (optional)
   192      "nameservers": <list-of-nameservers>                     (optional)
   193      "domain": <name-of-local-domain>                         (optional)
   194      "search": <list-of-additional-search-domains>            (optional)
   195      "options": <list-of-options>                             (optional)
   196    }
   197  }
   198  ```
   199  
   200  `cniVersion` specifies a [Semantic Version 2.0](https://semver.org) of CNI specification used by the plugin. A plugin may support multiple CNI spec versions (as it reports via the `VERSION` command), here the `cniVersion` returned by the plugin in the result must be consistent with the `cniVersion` specified in [Network Configuration](#network-configuration). If the `cniVersion` in the network configuration is not supported by the plugin, the plugin should return an error code 1 (see [Well-known Error Codes](#well-known-error-codes) for details).
   201  
   202  `interfaces` describes specific network interfaces the plugin created.
   203  If the `CNI_IFNAME` variable exists the plugin must use that name for the sandbox/hypervisor interface or return an error if it cannot.
   204  - `mac` (string): the hardware address of the interface.
   205     If L2 addresses are not meaningful for the plugin then this field is optional.
   206  - `sandbox` (string): container/namespace-based environments should return the full filesystem path to the network namespace of that sandbox.
   207     Hypervisor/VM-based plugins should return an ID unique to the virtualized sandbox the interface was created in.
   208     This item must be provided for interfaces created or moved into a sandbox like a network namespace or a hypervisor/VM.
   209  
   210  The `ips` field is a list of IP configuration information.
   211  See the [IP well-known structure](#ips) section for more information.
   212  
   213  The `routes` field is a list of route configuration information.
   214  See the [Routes well-known structure](#routes) section for more information.
   215  
   216  The `dns` field contains a dictionary consisting of common DNS information.
   217  See the [DNS well-known structure](#dns) section for more information.
   218  
   219  The specification does not declare how this information must be processed by CNI consumers.
   220  Examples include generating an `/etc/resolv.conf` file to be injected into the container filesystem or running a DNS forwarder on the host.
   221  
   222  Errors must be indicated by a non-zero return code and the following JSON being printed to stdout:
   223  ```
   224  {
   225    "cniVersion": "0.4.0",
   226    "code": <numeric-error-code>,
   227    "msg": <short-error-message>,
   228    "details": <long-error-message> (optional)
   229  }
   230  ```
   231  
   232  `cniVersion` specifies a [Semantic Version 2.0](https://semver.org) of CNI specification used by the plugin.
   233  Error codes 0-99 are reserved for well-known errors (see [Well-known Error Codes](#well-known-error-codes) section).
   234  Values of 100+ can be freely used for plugin specific errors. 
   235  
   236  In addition, stderr can be used for unstructured output such as logs.
   237  
   238  ### Network Configuration
   239  
   240  The network configuration is described in JSON form. The configuration may be stored on disk or generated from other sources by the container runtime. The following fields are well-known and have the following meaning:
   241  - `cniVersion` (string): [Semantic Version 2.0](https://semver.org) of CNI specification to which this configuration conforms.
   242  - `name` (string): Network name. This should be unique across all containers on the host (or other administrative domain).  Must start with a alphanumeric character, optionally followed by any combination of one or more alphanumeric characters, underscore (_), dot (.) or hyphen (-).
   243  - `type` (string): Refers to the filename of the CNI plugin executable.
   244  - `args` (dictionary, optional): Additional arguments provided by the container runtime. For example a dictionary of labels could be passed to CNI plugins by adding them to a labels field under `args`.
   245  - `ipMasq` (boolean, optional): If supported by the plugin, sets up an IP masquerade on the host for this network. This is necessary if the host will act as a gateway to subnets that are not able to route to the IP assigned to the container.
   246  - `ipam` (dictionary, optional): Dictionary with IPAM specific values:
   247    - `type` (string): Refers to the filename of the IPAM plugin executable.
   248  - `dns` (dictionary, optional): Dictionary with DNS specific values:
   249    - `nameservers` (list of strings, optional): list of a priority-ordered list of DNS nameservers that this network is aware of. Each entry in the list is a string containing either an IPv4 or an IPv6 address.
   250    - `domain` (string, optional): the local domain used for short hostname lookups.
   251    - `search` (list of strings, optional): list of priority ordered search domains for short hostname lookups. Will be preferred over `domain` by most resolvers.
   252    - `options` (list of strings, optional): list of options that can be passed to the resolver
   253  
   254  Plugins may define additional fields that they accept and may generate an error if called with unknown fields. The exception to this is the `args` field may be used to pass arbitrary data which should be ignored by plugins if not understood.
   255  
   256  ### Example configurations
   257  
   258  ```jsonc
   259  {
   260    "cniVersion": "0.4.0",
   261    "name": "dbnet",
   262    "type": "bridge",
   263    // type (plugin) specific
   264    "bridge": "cni0",
   265    "ipam": {
   266      "type": "host-local",
   267      // ipam specific
   268      "subnet": "10.1.0.0/16",
   269      "gateway": "10.1.0.1"
   270    },
   271    "dns": {
   272      "nameservers": [ "10.1.0.1" ]
   273    }
   274  }
   275  ```
   276  
   277  ```jsonc
   278  {
   279    "cniVersion": "0.4.0",
   280    "name": "pci",
   281    "type": "ovs",
   282    // type (plugin) specific
   283    "bridge": "ovs0",
   284    "vxlanID": 42,
   285    "ipam": {
   286      "type": "dhcp",
   287      "routes": [ { "dst": "10.3.0.0/16" }, { "dst": "10.4.0.0/16" } ]
   288    },
   289    // args may be ignored by plugins
   290    "args": {
   291      "labels" : {
   292          "appVersion" : "1.0"
   293      }
   294    }
   295  }
   296  ```
   297  
   298  ```jsonc
   299  {
   300    "cniVersion": "0.4.0",
   301    "name": "wan",
   302    "type": "macvlan",
   303    // ipam specific
   304    "ipam": {
   305      "type": "dhcp",
   306      "routes": [ { "dst": "10.0.0.0/8", "gw": "10.0.0.1" } ]
   307    },
   308    "dns": {
   309      "nameservers": [ "10.0.0.1" ]
   310    }
   311  }
   312  ```
   313  
   314  ### Network Configuration Lists
   315  
   316  Network configuration lists provide a mechanism to run multiple CNI plugins for a single container in a defined order, passing the result of each plugin to the next plugin.
   317  The list is composed of well-known fields and list of one or more standard CNI network configurations (see above).
   318  
   319  The list is described in JSON form, and can be stored on disk or generated from other sources by the container runtime. The following fields are well-known and have the following meaning:
   320  - `cniVersion` (string): [Semantic Version 2.0](https://semver.org) of CNI specification to which this configuration list and all the individual configurations conform.
   321  - `name` (string): Network name. This should be unique across all containers on the host (or other administrative domain).  Must start with a alphanumeric character, optionally followed by any combination of one or more alphanumeric characters, underscore (_), dot (.) or hyphen (-).
   322  - `disableCheck` (string): Either `true` or `false`.  If `disableCheck` is `true`, runtimes must not call `CHECK` for this network configuration list.  This allows an administrator to prevent `CHECK`ing where a combination of plugins is known to return spurious errors.
   323  - `plugins` (list): A list of standard CNI network configuration dictionaries (see above).
   324  
   325  When executing a plugin list, the runtime MUST replace the `name` and `cniVersion` fields in each individual network configuration in the list with the `name` and `cniVersion` field of the list itself. This ensures that the name and CNI version is the same for all plugin executions in the list, preventing versioning conflicts between plugins.
   326  The runtime may also pass capability-based keys as a map in the top-level `runtimeConfig` key of the plugin's config JSON if a plugin advertises it supports a specific capability via the `capabilities` key of its network configuration.  The key passed in `runtimeConfig` MUST match the name of the specific capability from the `capabilities` key of the plugins network configuration. See CONVENTIONS.md for more information on capabilities and how they are sent to plugins via the `runtimeConfig` key.
   327  
   328  For the `ADD` action, the runtime MUST also add a `prevResult` field to the configuration JSON of any plugin after the first one, which MUST be the `Result` of the previous plugin (if any) in JSON format ([see below](#network-configuration-list-runtime-examples)).
   329  For the `CHECK` and `DEL` actions, the runtime MUST (except that it may be omitted for `DEL` if not available) add a `prevResult` field to the configuration JSON of each plugin, which MUST be the `Result` of the immediately previous `ADD` action in JSON format ([see below](#network-configuration-list-runtime-examples)).
   330  For the `ADD` action, plugins SHOULD echo the contents of the `prevResult` field to their stdout to allow subsequent plugins (and the runtime) to receive the result, unless they wish to modify or suppress a previous result.
   331  Plugins are allowed to modify or suppress all or part of a `prevResult`.
   332  However, plugins that support a version of the CNI specification that includes the `prevResult` field MUST handle `prevResult` by either passing it through, modifying it, or suppressing it explicitly.
   333  It is a violation of this specification to be unaware of the `prevResult` field.
   334  
   335  The runtime MUST also execute each plugin in the list with the same environment.
   336  
   337  For the `DEL` action, the runtime MUST execute the plugins in reverse-order.
   338  
   339  #### Network Configuration List Error Handling
   340  
   341  When an error occurs while executing an action on a plugin list (eg, either `ADD` or `DEL`) the runtime MUST stop execution of the list.
   342  
   343  If an `ADD` action fails, when the runtime decides to handle the failure it should execute the `DEL` action (in reverse order from the `ADD` as specified above) for all plugins in the list, even if some were not called during the `ADD` action.
   344  
   345  #### Example network configuration lists
   346  
   347  ```jsonc
   348  {
   349    "cniVersion": "0.4.0",
   350    "name": "dbnet",
   351    "plugins": [
   352      {
   353        "type": "bridge",
   354        // type (plugin) specific
   355        "bridge": "cni0",
   356        // args may be ignored by plugins
   357        "args": {
   358          "labels" : {
   359              "appVersion" : "1.0"
   360          }
   361        },
   362        "ipam": {
   363          "type": "host-local",
   364          // ipam specific
   365          "subnet": "10.1.0.0/16",
   366          "gateway": "10.1.0.1"
   367        },
   368        "dns": {
   369          "nameservers": [ "10.1.0.1" ]
   370        }
   371      },
   372      {
   373        "type": "tuning",
   374        "sysctl": {
   375          "net.core.somaxconn": "500"
   376        }
   377      }
   378    ]
   379  }
   380  ```
   381  
   382  #### Network configuration list runtime examples
   383  
   384  Given the network configuration list JSON [shown above](#example-network-configuration-lists) the container runtime would perform the following steps for the `ADD` action.
   385  Note that the runtime adds the `cniVersion` and `name` fields from configuration list to the configuration JSON passed to each plugin, to ensure consistent versioning and names for all plugins in the list.
   386  
   387  1) first call the `bridge` plugin with the following JSON:
   388  
   389  ```jsonc
   390  {
   391    "cniVersion": "0.4.0",
   392    "name": "dbnet",
   393    "type": "bridge",
   394    "bridge": "cni0",
   395    "args": {
   396      "labels" : {
   397          "appVersion" : "1.0"
   398      }
   399    },
   400    "ipam": {
   401      "type": "host-local",
   402      // ipam specific
   403      "subnet": "10.1.0.0/16",
   404      "gateway": "10.1.0.1"
   405    },
   406    "dns": {
   407      "nameservers": [ "10.1.0.1" ]
   408    }
   409  }
   410  ```
   411  
   412  2) next call the `tuning` plugin with the following JSON, including the `prevResult` field containing the JSON response from the `bridge` plugin:
   413  
   414  ```json
   415  {
   416    "cniVersion": "0.4.0",
   417    "name": "dbnet",
   418    "type": "tuning",
   419    "sysctl": {
   420      "net.core.somaxconn": "500"
   421    },
   422    "prevResult": {
   423      "ips": [
   424          {
   425            "version": "4",
   426            "address": "10.0.0.5/32",
   427            "interface": 2
   428          }
   429      ],
   430      "interfaces": [
   431          {
   432              "name": "cni0",
   433              "mac": "00:11:22:33:44:55",
   434          },
   435          {
   436              "name": "veth3243",
   437              "mac": "55:44:33:22:11:11",
   438          },
   439          {
   440              "name": "eth0",
   441              "mac": "99:88:77:66:55:44",
   442              "sandbox": "/var/run/netns/blue",
   443          }
   444      ],
   445      "dns": {
   446        "nameservers": [ "10.1.0.1" ]
   447      }
   448    }
   449  }
   450  ```
   451  
   452  Given the same network configuration JSON list, the container runtime would perform the following steps for the `CHECK` action.
   453  
   454  1) first call the `bridge` plugin with the following JSON, including the `prevResult` field containing the JSON response from the `ADD` operation:
   455  
   456  ```jsonc
   457  {
   458    "cniVersion": "0.4.0",
   459    "name": "dbnet",
   460    "type": "bridge",
   461    "bridge": "cni0",
   462    "args": {
   463      "labels" : {
   464          "appVersion" : "1.0"
   465      }
   466    },
   467    "ipam": {
   468      "type": "host-local",
   469      // ipam specific
   470      "subnet": "10.1.0.0/16",
   471      "gateway": "10.1.0.1"
   472    },
   473    "dns": {
   474      "nameservers": [ "10.1.0.1" ]
   475    },
   476    "prevResult": {
   477      "ips": [
   478          {
   479            "version": "4",
   480            "address": "10.0.0.5/32",
   481            "interface": 2
   482          }
   483      ],
   484      "interfaces": [
   485          {
   486              "name": "cni0",
   487              "mac": "00:11:22:33:44:55",
   488          },
   489          {
   490              "name": "veth3243",
   491              "mac": "55:44:33:22:11:11",
   492          },
   493          {
   494              "name": "eth0",
   495              "mac": "99:88:77:66:55:44",
   496              "sandbox": "/var/run/netns/blue",
   497          }
   498      ],
   499      "dns": {
   500        "nameservers": [ "10.1.0.1" ]
   501      }
   502    }
   503  }
   504  ```
   505  
   506  2) next call the `tuning` plugin with the following JSON, including the `prevResult` field containing the JSON response from the `ADD` operation:
   507  
   508  ```json
   509  {
   510    "cniVersion": "0.4.0",
   511    "name": "dbnet",
   512    "type": "tuning",
   513    "sysctl": {
   514      "net.core.somaxconn": "500"
   515    },
   516    "prevResult": {
   517      "ips": [
   518          {
   519            "version": "4",
   520            "address": "10.0.0.5/32",
   521            "interface": 2
   522          }
   523      ],
   524      "interfaces": [
   525          {
   526              "name": "cni0",
   527              "mac": "00:11:22:33:44:55",
   528          },
   529          {
   530              "name": "veth3243",
   531              "mac": "55:44:33:22:11:11",
   532          },
   533          {
   534              "name": "eth0",
   535              "mac": "99:88:77:66:55:44",
   536              "sandbox": "/var/run/netns/blue",
   537          }
   538      ],
   539      "dns": {
   540        "nameservers": [ "10.1.0.1" ]
   541      }
   542    }
   543  }
   544  ```
   545  
   546  Given the same network configuration JSON list, the container runtime would perform the following steps for the `DEL` action.
   547  Note that plugins are executed in reverse order from the `ADD` and `CHECK` actions.
   548  
   549  1) first call the `tuning` plugin with the following JSON, including the `prevResult` field containing the JSON response from the `ADD` action:
   550  
   551  ```json
   552  {
   553    "cniVersion": "0.4.0",
   554    "name": "dbnet",
   555    "type": "tuning",
   556    "sysctl": {
   557      "net.core.somaxconn": "500"
   558    },
   559    "prevResult": {
   560      "ips": [
   561          {
   562            "version": "4",
   563            "address": "10.0.0.5/32",
   564            "interface": 2
   565          }
   566      ],
   567      "interfaces": [
   568          {
   569              "name": "cni0",
   570              "mac": "00:11:22:33:44:55",
   571          },
   572          {
   573              "name": "veth3243",
   574              "mac": "55:44:33:22:11:11",
   575          },
   576          {
   577              "name": "eth0",
   578              "mac": "99:88:77:66:55:44",
   579              "sandbox": "/var/run/netns/blue",
   580          }
   581      ],
   582      "dns": {
   583        "nameservers": [ "10.1.0.1" ]
   584      }
   585    }
   586  }
   587  ```
   588  
   589  2) next call the `bridge` plugin with the following JSON, including the `prevResult` field containing the JSON response from the `ADD` action:
   590  
   591  ```jsonc
   592  {
   593    "cniVersion": "0.4.0",
   594    "name": "dbnet",
   595    "type": "bridge",
   596    "bridge": "cni0",
   597    "args": {
   598      "labels" : {
   599          "appVersion" : "1.0"
   600      }
   601    },
   602    "ipam": {
   603      "type": "host-local",
   604      // ipam specific
   605      "subnet": "10.1.0.0/16",
   606      "gateway": "10.1.0.1"
   607    },
   608    "dns": {
   609      "nameservers": [ "10.1.0.1" ]
   610    },
   611    "prevResult": {
   612      "ips": [
   613          {
   614            "version": "4",
   615            "address": "10.0.0.5/32",
   616            "interface": 2
   617          }
   618      ],
   619      "interfaces": [
   620          {
   621              "name": "cni0",
   622              "mac": "00:11:22:33:44:55",
   623          },
   624          {
   625              "name": "veth3243",
   626              "mac": "55:44:33:22:11:11",
   627          },
   628          {
   629              "name": "eth0",
   630              "mac": "99:88:77:66:55:44",
   631              "sandbox": "/var/run/netns/blue",
   632          }
   633      ],
   634      "dns": {
   635        "nameservers": [ "10.1.0.1" ]
   636      }
   637    }
   638  }
   639  ```
   640  
   641  ### IP Allocation
   642  
   643  As part of its operation, a CNI plugin is expected to assign (and maintain) an IP address to the interface and install any necessary routes relevant for that interface. This gives the CNI plugin great flexibility but also places a large burden on it. Many CNI plugins would need to have the same code to support several IP management schemes that users may desire (e.g. dhcp, host-local). 
   644  
   645  To lessen the burden and make IP management strategy be orthogonal to the type of CNI plugin, we define a second type of plugin -- IP Address Management Plugin (IPAM plugin). It is however the responsibility of the CNI plugin to invoke the IPAM plugin at the proper moment in its execution. The IPAM plugin must determine the interface IP/subnet, Gateway and Routes and return this information to the "main" plugin to apply. The IPAM plugin may obtain the information via a protocol (e.g. dhcp), data stored on a local filesystem, the "ipam" section of the Network Configuration file or a combination of the above.
   646  
   647  #### IP Address Management (IPAM) Interface
   648  
   649  Like CNI plugins, the IPAM plugins are invoked by running an executable. The executable is searched for in a predefined list of paths, indicated to the CNI plugin via `CNI_PATH`. The IPAM Plugin must receive all the same environment variables that were passed in to the CNI plugin. Just like the CNI plugin, IPAM plugins receive the network configuration via stdin.
   650  
   651  Success must be indicated by a zero return code and the following JSON being printed to stdout (in the case of the ADD command):
   652  
   653  ```
   654  {
   655    "cniVersion": "0.4.0",
   656    "ips": [
   657        {
   658            "version": "<4-or-6>",
   659            "address": "<ip-and-prefix-in-CIDR>",
   660            "gateway": "<ip-address-of-the-gateway>"  (optional)
   661        },
   662        ...
   663    ],
   664    "routes": [                                       (optional)
   665        {
   666            "dst": "<ip-and-prefix-in-cidr>",
   667            "gw": "<ip-of-next-hop>"                  (optional)
   668        },
   669        ...
   670    ]
   671    "dns": {                                          (optional)
   672      "nameservers": <list-of-nameservers>            (optional)
   673      "domain": <name-of-local-domain>                (optional)
   674      "search": <list-of-search-domains>              (optional)
   675      "options": <list-of-options>                    (optional)
   676    }
   677  }
   678  ```
   679  
   680  Note that unlike regular CNI plugins, IPAM plugins should return an abbreviated `Result` structure that does not include the `interfaces` key, since IPAM plugins should be unaware of interfaces configured by their parent plugin except those specifically required for IPAM (eg, like the `dhcp` IPAM plugin).
   681  
   682  `cniVersion` specifies a [Semantic Version 2.0](https://semver.org) of CNI specification used by the IPAM plugin. An IPAM plugin may support multiple CNI spec versions (as it reports via the `VERSION` command), here the `cniVersion` returned by the IPAM plugin in the result must be consistent with the `cniVersion` specified in [Network Configuration](#network-configuration). If the `cniVersion` in the network configuration is not supported by the IPAM plugin, the plugin should return an error code 1 (see [Well-known Error Codes](#well-known-error-codes) for details).
   683  
   684  The `ips` field is a list of IP configuration information.
   685  See the [IP well-known structure](#ips) section for more information.
   686  
   687  The `routes` field is a list of route configuration information.
   688  See the [Routes well-known structure](#routes) section for more information.
   689  
   690  The `dns` field contains a dictionary consisting of common DNS information.
   691  See the [DNS well-known structure](#dns) section for more information.
   692  
   693  Errors and logs are communicated in the same way as the CNI plugin. See [CNI Plugin Result](#result) section for details.
   694  
   695  IPAM plugin examples:
   696   - **host-local**: Select an unused (by other containers on the same host) IP within the specified range.
   697   - **dhcp**: Use DHCP protocol to acquire and maintain a lease. The DHCP requests will be sent via the created container interface; therefore, the associated network must support broadcast.
   698  
   699  #### Notes
   700   - Routes are expected to be added with a 0 metric.
   701   - A default route may be specified via "0.0.0.0/0". Since another network might have already configured the default route, the CNI plugin should be prepared to skip over its default route definition.
   702  
   703  ### Well-known Structures
   704  
   705  #### IPs
   706  
   707  ```
   708    "ips": [
   709        {
   710            "version": "<4-or-6>",
   711            "address": "<ip-and-prefix-in-CIDR>",
   712            "gateway": "<ip-address-of-the-gateway>",      (optional)
   713            "interface": <numeric index into 'interfaces' list> (not required for IPAM plugins)
   714        },
   715        ...
   716    ]
   717  ```
   718  
   719  The `ips` field is a list of IP configuration information determined by the plugin. Each item is a dictionary describing of IP configuration for a network interface.
   720  IP configuration for multiple network interfaces and multiple IP configurations for a single interface may be returned as separate items in the `ips` list.
   721  All properties known to the plugin should be provided, even if not strictly required.
   722  - `version` (string): either "4" or "6" and corresponds to the IP version of the addresses in the entry.
   723     All IP addresses and gateways provided must be valid for the given `version`.
   724  - `address` (string): an IP address in CIDR notation (eg "192.168.1.3/24").
   725  - `gateway` (string): the default gateway for this subnet, if one exists.
   726     It does not instruct the CNI plugin to add any routes with this gateway: routes to add are specified separately via the `routes` field.
   727     An example use of this value is for the CNI `bridge` plugin to add this IP address to the Linux bridge to make it a gateway.
   728  - `interface` (uint): the index into the `interfaces` list for a [CNI Plugin Result](#result) indicating which interface this IP configuration should be applied to.
   729     IPAM plugins should not return this key since they have no information about network interfaces.
   730  
   731  #### Routes
   732  
   733  ```
   734    "routes": [
   735        {
   736            "dst": "<ip-and-prefix-in-cidr>",
   737            "gw": "<ip-of-next-hop>"               (optional)
   738        },
   739        ...
   740    ]
   741  ```
   742  
   743  Each `routes` entry is a dictionary with the following fields.  All IP addresses in the `routes` entry must be the same IP version, either 4 or 6.
   744    - `dst` (string): destination subnet specified in CIDR notation.
   745    - `gw` (string): IP of the gateway. If omitted, a default gateway is assumed (as determined by the CNI plugin).
   746  
   747  Each `routes` entry must be relevant for the sandbox interface specified by CNI_IFNAME.
   748  
   749  #### DNS
   750  
   751  ```
   752    "dns": {
   753      "nameservers": <list-of-nameservers>                 (optional)
   754      "domain": <name-of-local-domain>                     (optional)
   755      "search": <list-of-additional-search-domains>        (optional)
   756      "options": <list-of-options>                         (optional)
   757    }
   758  ```
   759  
   760  The `dns` field contains a dictionary consisting of common DNS information.
   761  - `nameservers` (list of strings): list of a priority-ordered list of DNS nameservers that this network is aware of. Each entry in the list is a string containing either an IPv4 or an IPv6 address.
   762  - `domain` (string): the local domain used for short hostname lookups.
   763  - `search` (list of strings): list of priority ordered search domains for short hostname lookups. Will be preferred over `domain` by most resolvers.
   764  - `options` (list of strings): list of options that can be passed to the resolver.
   765    See [CNI Plugin Result](#result) section for more information.
   766    
   767  ## Well-known Error Codes
   768  
   769  Error codes 1-99 must not be used other than as specified here.
   770  
   771  - `1` - Incompatible CNI version
   772  - `2` - Unsupported field in network configuration. The error message must contain the key and value of the unsupported field.
   773  - `3` - Container unknown or does not exist. This error implies the runtime does not need to perform any container network cleanup (for example, calling the `DEL` action on the container).
   774  - `4` - Invalid necessary environment variables, like CNI_COMMAND, CNI_CONTAINERID, etc. The error message must contain the names of invalid variables.
   775  - `5` - I/O failure. For example, failed to read network config bytes from stdin.
   776  - `6` - Failed to decode content. For example, failed to unmarshal network config from bytes or failed to decode version info from string.
   777  - `7` - Invalid network config. If some validations on network configs do not pass, this error will be raised.
   778  - `11` - Try again later. If the plugin detects some transient condition that should clear up, it can use this code to notify the runtime it should re-try the operation later.