Updated on 2026-07-29 GMT+08:00

Creating a Traffic Mirror Filter Rule

Function

This API is used to add inbound and outbound rules to a traffic mirror filter.

  • Inbound rules match the traffic received by a mirror source.

  • Outbound rules match the traffic sent by a mirror source.

Calling Method

For details, see Calling APIs.

Authorization Information

Each account has all the permissions required to call all APIs, but IAM users must be assigned the required permissions.

  • If you are using role/policy-based authorization, see Permissions Policies and Supported Actions for details on the required permissions.
  • If you are using identity policy-based authorization, the following identity policy-based permissions are required.

    Action

    Access Level

    Resource Type (*: required)

    Condition Key

    Alias

    Dependencies

    vpc:trafficMirrorFilterRules:create

    Write

    trafficMirrorFilter *

    -

    -

    -

    trafficMirrorFilterRule *

    -

URI

POST /v3/{project_id}/vpc/traffic-mirror-filter-rules

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Definition

ID of the project that a traffic mirror filter rule belongs to. For details about how to obtain a project ID, see Obtaining a Project ID.

Constraints

N/A

Range

N/A

Default Value

N/A

Request Parameters

Table 2 Request body parameters

Parameter

Mandatory

Type

Description

traffic_mirror_filter_rule

Yes

CreateTrafficMirrorFilterRuleOption object

Definition

Traffic mirror filter rule.

Constraints

N/A

Range

N/A

Default Value

N/A

Table 3 CreateTrafficMirrorFilterRuleOption

Parameter

Mandatory

Type

Description

description

No

String

Definition

Description of a traffic mirror filter rule.

Constraints

The value can contain 0 to 255 characters and cannot contain angle brackets (< or >).

Range

N/A

Default Value

N/A

traffic_mirror_filter_id

Yes

String

Definition

Traffic mirror filter ID.

Constraints

N/A

Range

N/A

Default Value

N/A

direction

Yes

String

Definition

Traffic direction.

Constraints

N/A

Range

  • ingress: inbound traffic

  • egress: outbound traffic

Default Value

N/A

protocol

Yes

String

Definition

Network protocol type supported by the mirrored traffic.

Constraints

N/A

Range

  • TCP: If you select TCP, you can customize the source and destination port ranges.

  • UDP: If you select UDP, you can customize the source and destination port ranges.

  • ICMP: If you set IP address version to IPv4 and select ICMP, all ports are specified for source and destination port ranges by default.

  • ICMPV6: If you set IP address version to IPv6 and select ICMPv6, all ports are specified for source and destination port ranges by default.

  • ALL: If you select ALL, all network protocols are supported and all ports are specified for source and destination port ranges by default.

Default Value

N/A

ethertype

Yes

String

Definition

Address protocol version of the mirrored traffic.

Constraints

N/A

Range

  • IPv4

  • IPv6

Default Value

N/A

source_cidr_block

No

String

Definition

Source address of the mirrored traffic.

Constraints

N/A

Range

  • A single IP address: IP address/mask

    • Example IPv4 address: 192.168.10.10/32

    • Example IPv6 address: 2002:50::44/128

  • An IP address range in CIDR notation: IP address/mask

    • Example IPv4 CIDR block: 192.168.52.0/24

    • Example IPv6 CIDR block: 2407:c080:802:469::/64

  • All IP addresses

    • 0.0.0.0/0 represents all IPv4 addresses.

    • ::/0 represents all IPv6 addresses.

Default Value

N/A

destination_cidr_block

No

String

Definition

Destination address of the mirrored traffic.

Constraints

N/A

Range

  • A single IP address: IP address/mask

    • Example IPv4 address: 192.168.10.10/32

    • Example IPv6 address: 2002:50::44/128

  • An IP address range in CIDR notation: IP address/mask

    • Example IPv4 CIDR block: 192.168.52.0/24

    • Example IPv6 CIDR block: 2407:c080:802:469::/64

  • All IP addresses

    • 0.0.0.0/0 represents all IPv4 addresses.

    • ::/0 represents all IPv6 addresses.

Default Value

N/A

source_port_range

No

String

Definition

Source port range of the traffic.

Constraints

  • Use a hyphen (-) to connect the start port and the end port, for example, 22-23. The end port cannot be smaller than the start port.

  • If not specified or 1-65535 is specified, all ports are used.

Range

1 to 65535

Default Value

N/A

destination_port_range

No

String

Definition

Destination port range of the traffic.

Constraints

  • Use a hyphen (-) to connect the start port and the end port, for example, 22-23. The end port cannot be smaller than the start port.

  • If not specified or 1-65535 is specified, all ports are used.

Range

1 to 65535

Default Value

N/A

action

Yes

String

Definition

Whether to accept or reject the mirrored traffic that meets the traffic mirror filter.

Constraints

N/A

Range

  • accept: The traffic is accepted and will be mirrored to the mirror target.

  • reject: The traffic is rejected and will not be mirrored to the mirror target.

Default Value

N/A

priority

Yes

Integer

Definition

Priority of a traffic mirror filter rule. A smaller value indicates a higher priority.

Constraints

N/A

Range

1 to 65535

Default Value

N/A

Response Parameters

Status code: 201

Table 4 Response body parameters

Parameter

Type

Description

traffic_mirror_filter_rule

TrafficMirrorFilterRule object

Definition:

Response body for querying details about a traffic mirror filter rule.

Range:

N/A

request_id

String

Definition:

Request ID.

Range:

N/A

Table 5 TrafficMirrorFilterRule

Parameter

Type

Description

id

String

Definition:

ID of a traffic mirror filter rule.

Range:

N/A

project_id

String

Definition:

ID of the project that a traffic mirror filter rule belongs to.

Range:

N/A

description

String

Definition

Description of a traffic mirror filter rule.

Range

N/A

traffic_mirror_filter_id

String

Definition:

ID of the traffic mirror filter that a traffic mirror filter rule belongs to.

Range:

N/A

direction

String

Definition:

Traffic direction of a traffic mirror filter rule.

Range:

  • ingress: inbound traffic

  • egress: outbound traffic

source_cidr_block

String

Definition

Source of a traffic mirror filter rule.

Range

  • A single IP address: IP address/mask

    • Example IPv4 address: 192.168.10.10/32

    • Example IPv6 address: 2002:50::44/128

  • An IP address range in CIDR notation: IP address/mask

    • Example IPv4 CIDR block: 192.168.52.0/24

    • Example IPv6 CIDR block: 2407:c080:802:469::/64

  • All IP addresses

    • 0.0.0.0/0 represents all IPv4 addresses.

    • ::/0 represents all IPv6 addresses.

destination_cidr_block

String

Definition

Destination of a traffic mirror filter rule.

Range

  • A single IP address: IP address/mask

    • Example IPv4 address: 192.168.10.10/32

    • Example IPv6 address: 2002:50::44/128

  • An IP address range in CIDR notation: IP address/mask

    • Example IPv4 CIDR block: 192.168.52.0/24

    • Example IPv6 CIDR block: 2407:c080:802:469::/64

  • All IP addresses

    • 0.0.0.0/0 represents all IPv4 addresses.

    • ::/0 represents all IPv6 addresses.

source_port_range

String

Definition

Source port range of a traffic mirror filter rule.

Range

1 to 65535

destination_port_range

String

Definition

Destination port range of a traffic mirror filter rule.

Range

1 to 65535

ethertype

String

Definition

IP address version.

Range

  • IPv4

  • IPv6

protocol

String

Definition:

Network protocol type supported by traffic mirroring.

Range:

  • tcp : If you select TCP, you can customize the source and destination port ranges.

  • udp: If you select UDP, you can customize the source and destination port ranges.

  • icmp: If you set the IP address version to IPv4 and select ICMP, all ports are specified for source and destination port ranges by default.

  • icmpv6: If you set the IP address version to IPv6 and select ICMPv6, all ports are specified for source and destination port ranges by default.

  • all: If you select all, all network protocols are supported and all ports are specified for source and destination port ranges by default.

action

String

Definition:

Action of a traffic mirror filter rule.

Range:

  • accept

  • reject

priority

Integer

Definition:

Priority of a traffic mirror filter rule.

Range:

The value is from 1 to 65535. A smaller value indicates a higher priority.

created_at

String

Definition

Time when a traffic mirror filter rule was created.

Range

N/A

updated_at

String

Definition

Time when a traffic mirror filter rule was updated.

Range

N/A

Example Requests

Add a traffic mirror filter rule in the inbound direction to the traffic mirror filter with ID of 417d7317-6c17-4428-a0f3-997d3e2293a0 and with source CIDR of 192.168.0.0/24.

POST http://{endpoint}/v3/{project_id}/vpc/traffic-mirror-filter-rules

{
  "traffic_mirror_filter_rule" : {
    "traffic_mirror_filter_id" : "417d7317-6c17-4428-a0f3-997d3e2293a0",
    "ethertype" : "IPv4",
    "direction" : "ingress",
    "protocol" : "icmp",
    "source_cidr_block" : "192.168.0.0/24",
    "action" : "accept",
    "priority" : 29
  }
}

Example Responses

Status code: 201

Created

{
  "request_id" : "8dec5453-1690-4378-a976-40ba5e6d62ff",
  "traffic_mirror_filter_rule" : {
    "created_at" : "2023-03-22T07:07:55Z",
    "updated_at" : "2023-03-22T07:07:55Z",
    "id" : "1be5f64b-49a1-427d-a49e-9619cfb0492c",
    "project_id" : "7365fcd452924e398ec4cc1fe39c0d12",
    "description" : "",
    "traffic_mirror_filter_id" : "417d7317-6c17-4428-a0f3-997d3e2293a0",
    "direction" : "ingress",
    "protocol" : "icmp",
    "ethertype" : "IPv4",
    "source_cidr_block" : "192.168.0.0/24",
    "action" : "accept",
    "priority" : 29
  }
}

SDK Sample Code

The SDK sample code is as follows.

Add a traffic mirror filter rule in the inbound direction to the traffic mirror filter with ID of 417d7317-6c17-4428-a0f3-997d3e2293a0 and with source CIDR of 192.168.0.0/24.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.vpc.v3.region.VpcRegion;
import com.huaweicloud.sdk.vpc.v3.*;
import com.huaweicloud.sdk.vpc.v3.model.*;


public class CreateTrafficMirrorFilterRuleSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");
        String projectId = "{project_id}";

        ICredential auth = new BasicCredentials()
                .withProjectId(projectId)
                .withAk(ak)
                .withSk(sk);

        VpcClient client = VpcClient.newBuilder()
                .withCredential(auth)
                .withRegion(VpcRegion.valueOf("<YOUR REGION>"))
                .build();
        CreateTrafficMirrorFilterRuleRequest request = new CreateTrafficMirrorFilterRuleRequest();
        CreateTrafficMirrorFilterRuleRequestBody body = new CreateTrafficMirrorFilterRuleRequestBody();
        CreateTrafficMirrorFilterRuleOption trafficMirrorFilterRulebody = new CreateTrafficMirrorFilterRuleOption();
        trafficMirrorFilterRulebody.withTrafficMirrorFilterId("417d7317-6c17-4428-a0f3-997d3e2293a0")
            .withDirection("ingress")
            .withProtocol("icmp")
            .withEthertype("IPv4")
            .withSourceCidrBlock("192.168.0.0/24")
            .withAction("accept")
            .withPriority(29);
        body.withTrafficMirrorFilterRule(trafficMirrorFilterRulebody);
        request.withBody(body);
        try {
            CreateTrafficMirrorFilterRuleResponse response = client.createTrafficMirrorFilterRule(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

Add a traffic mirror filter rule in the inbound direction to the traffic mirror filter with ID of 417d7317-6c17-4428-a0f3-997d3e2293a0 and with source CIDR of 192.168.0.0/24.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkvpc.v3.region.vpc_region import VpcRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkvpc.v3 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]
    projectId = "{project_id}"

    credentials = BasicCredentials(ak, sk, projectId)

    client = VpcClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(VpcRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CreateTrafficMirrorFilterRuleRequest()
        trafficMirrorFilterRulebody = CreateTrafficMirrorFilterRuleOption(
            traffic_mirror_filter_id="417d7317-6c17-4428-a0f3-997d3e2293a0",
            direction="ingress",
            protocol="icmp",
            ethertype="IPv4",
            source_cidr_block="192.168.0.0/24",
            action="accept",
            priority=29
        )
        request.body = CreateTrafficMirrorFilterRuleRequestBody(
            traffic_mirror_filter_rule=trafficMirrorFilterRulebody
        )
        response = client.create_traffic_mirror_filter_rule(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

Add a traffic mirror filter rule in the inbound direction to the traffic mirror filter with ID of 417d7317-6c17-4428-a0f3-997d3e2293a0 and with source CIDR of 192.168.0.0/24.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    vpc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/v3"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/v3/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/v3/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")
    projectId := "{project_id}"

    auth, err := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        WithProjectId(projectId).
        SafeBuild()

    if err != nil {
        fmt.Println(err)
        return
    }

    hcClient, err := vpc.VpcClientBuilder().
         WithRegion(region.ValueOf("<YOUR REGION>")).
         WithCredential(auth).
         SafeBuild()


    if err != nil {
        fmt.Println(err)
        return
    }

    client := vpc.NewVpcClient(hcClient)

    request := &model.CreateTrafficMirrorFilterRuleRequest{}
	sourceCidrBlockTrafficMirrorFilterRule:= "192.168.0.0/24"
	trafficMirrorFilterRulebody := &model.CreateTrafficMirrorFilterRuleOption{
		TrafficMirrorFilterId: "417d7317-6c17-4428-a0f3-997d3e2293a0",
		Direction: "ingress",
		Protocol: "icmp",
		Ethertype: "IPv4",
		SourceCidrBlock: &sourceCidrBlockTrafficMirrorFilterRule,
		Action: "accept",
		Priority: int32(29),
	}
	request.Body = &model.CreateTrafficMirrorFilterRuleRequestBody{
		TrafficMirrorFilterRule: trafficMirrorFilterRulebody,
	}
	response, err := client.CreateTrafficMirrorFilterRule(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

For SDK sample code of more programming languages, see the Sample Code tab in API Explorer. SDK sample code can be automatically generated.

Status Codes

Status Code

Description

201

Created

Error Codes

See Error Codes.