4 * Copyright 2017 Mellanox.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
33 #define MAX_PATTERN_NUM 4
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);
43 * create a flow rule that sends packets with matching src and dest ip
49 * The selected target queue.
51 * The src ip value to match the input packet.
53 * The mask to apply to the src ip.
55 * The dest ip value to match the input packet.
57 * The mask to apply to the dest ip.
59 * Perform verbose error reporting if not NULL.
62 * A flow if the rule could be created else return NULL.
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)
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;
83 memset(pattern, 0, sizeof(pattern));
84 memset(action, 0, sizeof(action));
87 * set the rule attribute.
88 * in this case only ingress packets will be checked.
90 memset(&attr, 0, sizeof(struct rte_flow_attr));
94 * create the action sequence.
95 * one action only, move packet to queue
98 action[0].type = RTE_FLOW_ACTION_TYPE_QUEUE;
99 action[0].conf = &queue;
100 action[1].type = RTE_FLOW_ACTION_TYPE_END;
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.
107 memset(ð_spec, 0, sizeof(struct rte_flow_item_eth));
108 memset(ð_mask, 0, sizeof(struct rte_flow_item_eth));
111 pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
112 pattern[0].spec = ð_spec;
113 pattern[0].mask = ð_mask;
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.
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;
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.
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;
141 /* the final level must be always type end */
142 pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
144 res = rte_flow_validate(port_id, &attr, pattern, action, error);
146 flow = rte_flow_create(port_id, &attr, pattern, action, error);