1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 Mellanox Technologies, Ltd
3 */
4
5 #define MAX_PATTERN_NUM 3
6 #define MAX_ACTION_NUM 2
7
8 struct rte_flow *
9 generate_ipv4_flow(uint16_t port_id, uint16_t rx_q,
10 uint32_t src_ip, uint32_t src_mask,
11 uint32_t dest_ip, uint32_t dest_mask,
12 struct rte_flow_error *error);
13
14
15 /**
16 * create a flow rule that sends packets with matching src and dest ip
17 * to selected queue.
18 *
19 * @param port_id
20 * The selected port.
21 * @param rx_q
22 * The selected target queue.
23 * @param src_ip
24 * The src ip value to match the input packet.
25 * @param src_mask
26 * The mask to apply to the src ip.
27 * @param dest_ip
28 * The dest ip value to match the input packet.
29 * @param dest_mask
30 * The mask to apply to the dest ip.
31 * @param[out] error
32 * Perform verbose error reporting if not NULL.
33 *
34 * @return
35 * A flow if the rule could be created else return NULL.
36 */
37
38 /* Function responsible for creating the flow rule. 8< */
39 struct rte_flow *
generate_ipv4_flow(uint16_t port_id,uint16_t rx_q,uint32_t src_ip,uint32_t src_mask,uint32_t dest_ip,uint32_t dest_mask,struct rte_flow_error * error)40 generate_ipv4_flow(uint16_t port_id, uint16_t rx_q,
41 uint32_t src_ip, uint32_t src_mask,
42 uint32_t dest_ip, uint32_t dest_mask,
43 struct rte_flow_error *error)
44 {
45 /* Declaring structs being used. 8< */
46 struct rte_flow_attr attr;
47 struct rte_flow_item pattern[MAX_PATTERN_NUM];
48 struct rte_flow_action action[MAX_ACTION_NUM];
49 struct rte_flow *flow = NULL;
50 struct rte_flow_action_queue queue = { .index = rx_q };
51 struct rte_flow_item_ipv4 ip_spec;
52 struct rte_flow_item_ipv4 ip_mask;
53 /* >8 End of declaring structs being used. */
54 int res;
55
56 memset(pattern, 0, sizeof(pattern));
57 memset(action, 0, sizeof(action));
58
59 /* Set the rule attribute, only ingress packets will be checked. 8< */
60 memset(&attr, 0, sizeof(struct rte_flow_attr));
61 attr.ingress = 1;
62 /* >8 End of setting the rule attribute. */
63
64 /*
65 * create the action sequence.
66 * one action only, move packet to queue
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
78 /* Set this level to allow all. 8< */
79 pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
80 /* >8 End of setting the first level of the pattern. */
81
82 /*
83 * setting the second level of the pattern (IP).
84 * in this example this is the level we care about
85 * so we set it according to the parameters.
86 */
87
88 /* Setting the second level of the pattern. 8< */
89 memset(&ip_spec, 0, sizeof(struct rte_flow_item_ipv4));
90 memset(&ip_mask, 0, sizeof(struct rte_flow_item_ipv4));
91 ip_spec.hdr.dst_addr = htonl(dest_ip);
92 ip_mask.hdr.dst_addr = dest_mask;
93 ip_spec.hdr.src_addr = htonl(src_ip);
94 ip_mask.hdr.src_addr = src_mask;
95 pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
96 pattern[1].spec = &ip_spec;
97 pattern[1].mask = &ip_mask;
98 /* >8 End of setting the second level of the pattern. */
99
100 /* The final level must be always type end. 8< */
101 pattern[2].type = RTE_FLOW_ITEM_TYPE_END;
102 /* >8 End of final level must be always type end. */
103
104 /* Validate the rule and create it. 8< */
105 res = rte_flow_validate(port_id, &attr, pattern, action, error);
106 if (!res)
107 flow = rte_flow_create(port_id, &attr, pattern, action, error);
108 /* >8 End of validation the rule and create it. */
109
110 return flow;
111 }
112 /* >8 End of function responsible for creating the flow rule. */
113