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(uint8_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(uint8_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_ipv4 ip_spec;
78 struct rte_flow_item_ipv4 ip_mask;
81 memset(pattern, 0, sizeof(pattern));
82 memset(action, 0, sizeof(action));
85 * set the rule attribute.
86 * in this case only ingress packets will be checked.
88 memset(&attr, 0, sizeof(struct rte_flow_attr));
92 * create the action sequence.
93 * one action only, move packet to queue
96 action[0].type = RTE_FLOW_ACTION_TYPE_QUEUE;
97 action[0].conf = &queue;
98 action[1].type = RTE_FLOW_ACTION_TYPE_END;
101 * set the first level of the pattern (eth).
102 * since in this example we just want to get the
103 * ipv4 we set this level to allow all.
105 memset(ð_spec, 0, sizeof(struct rte_flow_item_eth));
106 memset(ð_mask, 0, sizeof(struct rte_flow_item_eth));
109 pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
110 pattern[0].spec = ð_spec;
111 pattern[0].mask = ð_mask;
114 * setting the third level of the pattern (ip).
115 * in this example this is the level we care about
116 * so we set it according to the parameters.
118 memset(&ip_spec, 0, sizeof(struct rte_flow_item_ipv4));
119 memset(&ip_mask, 0, sizeof(struct rte_flow_item_ipv4));
120 ip_spec.hdr.dst_addr = htonl(dest_ip);
121 ip_mask.hdr.dst_addr = dest_mask;
122 ip_spec.hdr.src_addr = htonl(src_ip);
123 ip_mask.hdr.src_addr = src_mask;
124 pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
125 pattern[1].spec = &ip_spec;
126 pattern[1].mask = &ip_mask;
128 /* the final level must be always type end */
129 pattern[2].type = RTE_FLOW_ITEM_TYPE_END;
131 res = rte_flow_validate(port_id, &attr, pattern, action, error);
133 flow = rte_flow_create(port_id, &attr, pattern, action, error);