New upstream version 18.11-rc2
[deb_dpdk.git] / examples / flow_filtering / flow_blocks.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 Mellanox Technologies, Ltd
3  */
4
5 #define MAX_PATTERN_NUM         4
6
7 struct rte_flow *
8 generate_ipv4_flow(uint16_t port_id, uint16_t rx_q,
9                 uint32_t src_ip, uint32_t src_mask,
10                 uint32_t dest_ip, uint32_t dest_mask,
11                 struct rte_flow_error *error);
12
13
14 /**
15  * create a flow rule that sends packets with matching src and dest ip
16  * to selected queue.
17  *
18  * @param port_id
19  *   The selected port.
20  * @param rx_q
21  *   The selected target queue.
22  * @param src_ip
23  *   The src ip value to match the input packet.
24  * @param src_mask
25  *   The mask to apply to the src ip.
26  * @param dest_ip
27  *   The dest ip value to match the input packet.
28  * @param dest_mask
29  *   The mask to apply to the dest ip.
30  * @param[out] error
31  *   Perform verbose error reporting if not NULL.
32  *
33  * @return
34  *   A flow if the rule could be created else return NULL.
35  */
36 struct rte_flow *
37 generate_ipv4_flow(uint16_t port_id, uint16_t rx_q,
38                 uint32_t src_ip, uint32_t src_mask,
39                 uint32_t dest_ip, uint32_t dest_mask,
40                 struct rte_flow_error *error)
41 {
42         struct rte_flow_attr attr;
43         struct rte_flow_item pattern[MAX_PATTERN_NUM];
44         struct rte_flow_action action[MAX_PATTERN_NUM];
45         struct rte_flow *flow = NULL;
46         struct rte_flow_action_queue queue = { .index = rx_q };
47         struct rte_flow_item_eth eth_spec;
48         struct rte_flow_item_eth eth_mask;
49         struct rte_flow_item_ipv4 ip_spec;
50         struct rte_flow_item_ipv4 ip_mask;
51         int res;
52
53         memset(pattern, 0, sizeof(pattern));
54         memset(action, 0, sizeof(action));
55
56         /*
57          * set the rule attribute.
58          * in this case only ingress packets will be checked.
59          */
60         memset(&attr, 0, sizeof(struct rte_flow_attr));
61         attr.ingress = 1;
62
63         /*
64          * create the action sequence.
65          * one action only,  move packet to queue
66          */
67
68         action[0].type = RTE_FLOW_ACTION_TYPE_QUEUE;
69         action[0].conf = &queue;
70         action[1].type = RTE_FLOW_ACTION_TYPE_END;
71
72         /*
73          * set the first level of the pattern (eth).
74          * since in this example we just want to get the
75          * ipv4 we set this level to allow all.
76          */
77         memset(&eth_spec, 0, sizeof(struct rte_flow_item_eth));
78         memset(&eth_mask, 0, sizeof(struct rte_flow_item_eth));
79         eth_spec.type = 0;
80         eth_mask.type = 0;
81         pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
82         pattern[0].spec = &eth_spec;
83         pattern[0].mask = &eth_mask;
84
85         /*
86          * setting the third level of the pattern (ip).
87          * in this example this is the level we care about
88          * so we set it according to the parameters.
89          */
90         memset(&ip_spec, 0, sizeof(struct rte_flow_item_ipv4));
91         memset(&ip_mask, 0, sizeof(struct rte_flow_item_ipv4));
92         ip_spec.hdr.dst_addr = htonl(dest_ip);
93         ip_mask.hdr.dst_addr = dest_mask;
94         ip_spec.hdr.src_addr = htonl(src_ip);
95         ip_mask.hdr.src_addr = src_mask;
96         pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
97         pattern[1].spec = &ip_spec;
98         pattern[1].mask = &ip_mask;
99
100         /* the final level must be always type end */
101         pattern[2].type = RTE_FLOW_ITEM_TYPE_END;
102
103         res = rte_flow_validate(port_id, &attr, pattern, action, error);
104         if (!res)
105                 flow = rte_flow_create(port_id, &attr, pattern, action, error);
106
107         return flow;
108 }
109