New upstream version 18.02
[deb_dpdk.git] / examples / flow_filtering / flow_blocks.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 Mellanox.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Mellanox nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #define MAX_PATTERN_NUM         4
34
35 struct rte_flow *
36 generate_ipv4_flow(uint16_t port_id, uint16_t rx_q,
37                 uint32_t src_ip, uint32_t src_mask,
38                 uint32_t dest_ip, uint32_t dest_mask,
39                 struct rte_flow_error *error);
40
41
42 /**
43  * create a flow rule that sends packets with matching src and dest ip
44  * to selected queue.
45  *
46  * @param port_id
47  *   The selected port.
48  * @param rx_q
49  *   The selected target queue.
50  * @param src_ip
51  *   The src ip value to match the input packet.
52  * @param src_mask
53  *   The mask to apply to the src ip.
54  * @param dest_ip
55  *   The dest ip value to match the input packet.
56  * @param dest_mask
57  *   The mask to apply to the dest ip.
58  * @param[out] error
59  *   Perform verbose error reporting if not NULL.
60  *
61  * @return
62  *   A flow if the rule could be created else return NULL.
63  */
64 struct rte_flow *
65 generate_ipv4_flow(uint16_t port_id, uint16_t rx_q,
66                 uint32_t src_ip, uint32_t src_mask,
67                 uint32_t dest_ip, uint32_t dest_mask,
68                 struct rte_flow_error *error)
69 {
70         struct rte_flow_attr attr;
71         struct rte_flow_item pattern[MAX_PATTERN_NUM];
72         struct rte_flow_action action[MAX_PATTERN_NUM];
73         struct rte_flow *flow = NULL;
74         struct rte_flow_action_queue queue = { .index = rx_q };
75         struct rte_flow_item_eth eth_spec;
76         struct rte_flow_item_eth eth_mask;
77         struct rte_flow_item_vlan vlan_spec;
78         struct rte_flow_item_vlan vlan_mask;
79         struct rte_flow_item_ipv4 ip_spec;
80         struct rte_flow_item_ipv4 ip_mask;
81         int res;
82
83         memset(pattern, 0, sizeof(pattern));
84         memset(action, 0, sizeof(action));
85
86         /*
87          * set the rule attribute.
88          * in this case only ingress packets will be checked.
89          */
90         memset(&attr, 0, sizeof(struct rte_flow_attr));
91         attr.ingress = 1;
92
93         /*
94          * create the action sequence.
95          * one action only,  move packet to queue
96          */
97
98         action[0].type = RTE_FLOW_ACTION_TYPE_QUEUE;
99         action[0].conf = &queue;
100         action[1].type = RTE_FLOW_ACTION_TYPE_END;
101
102         /*
103          * set the first level of the pattern (eth).
104          * since in this example we just want to get the
105          * ipv4 we set this level to allow all.
106          */
107         memset(&eth_spec, 0, sizeof(struct rte_flow_item_eth));
108         memset(&eth_mask, 0, sizeof(struct rte_flow_item_eth));
109         eth_spec.type = 0;
110         eth_mask.type = 0;
111         pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
112         pattern[0].spec = &eth_spec;
113         pattern[0].mask = &eth_mask;
114
115         /*
116          * setting the second level of the pattern (vlan).
117          * since in this example we just want to get the
118          * ipv4 we also set this level to allow all.
119          */
120         memset(&vlan_spec, 0, sizeof(struct rte_flow_item_vlan));
121         memset(&vlan_mask, 0, sizeof(struct rte_flow_item_vlan));
122         pattern[1].type = RTE_FLOW_ITEM_TYPE_VLAN;
123         pattern[1].spec = &vlan_spec;
124         pattern[1].mask = &vlan_mask;
125
126         /*
127          * setting the third level of the pattern (ip).
128          * in this example this is the level we care about
129          * so we set it according to the parameters.
130          */
131         memset(&ip_spec, 0, sizeof(struct rte_flow_item_ipv4));
132         memset(&ip_mask, 0, sizeof(struct rte_flow_item_ipv4));
133         ip_spec.hdr.dst_addr = htonl(dest_ip);
134         ip_mask.hdr.dst_addr = dest_mask;
135         ip_spec.hdr.src_addr = htonl(src_ip);
136         ip_mask.hdr.src_addr = src_mask;
137         pattern[2].type = RTE_FLOW_ITEM_TYPE_IPV4;
138         pattern[2].spec = &ip_spec;
139         pattern[2].mask = &ip_mask;
140
141         /* the final level must be always type end */
142         pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
143
144         res = rte_flow_validate(port_id, &attr, pattern, action, error);
145         if (!res)
146                 flow = rte_flow_create(port_id, &attr, pattern, action, error);
147
148         return flow;
149 }
150